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

ARM64 - Add GT_CHK_DIV_BY_ZERO #69170

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions src/coreclr/jit/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void genCodeForBfiz(GenTreeOp* tree);
void genCodeForAddEx(GenTreeOp* tree);
void genCodeForCond(GenTreeOp* tree);
void genCodeForChkDivByZero(GenTreeOp* tree);
#endif // TARGET_ARM64

#if defined(FEATURE_EH_FUNCLETS)
Expand Down
41 changes: 18 additions & 23 deletions src/coreclr/jit/codegenarm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3169,10 +3169,6 @@ void CodeGen::genCodeForDivMod(GenTreeOp* tree)

if (divisorOp->IsIntegralConst(0))
{
// We unconditionally throw a divide by zero exception
genJumpToThrowHlpBlk(EJ_jmp, SCK_DIV_BY_ZERO);

// We still need to call genProduceReg
genProduceReg(tree);
}
else // the divisor is not the constant zero
Expand Down Expand Up @@ -3202,12 +3198,6 @@ void CodeGen::genCodeForDivMod(GenTreeOp* tree)
checkDividend = false; // We statically know that the dividend is not -1
}
}
else // insert check for divison by zero
{
// Check if the divisor is zero throw a DivideByZeroException
emit->emitIns_R_I(INS_cmp, size, divisorReg, 0);
genJumpToThrowHlpBlk(EJ_eq, SCK_DIV_BY_ZERO);
}

if (checkDividend)
{
Expand All @@ -3234,19 +3224,6 @@ void CodeGen::genCodeForDivMod(GenTreeOp* tree)
}
else // (tree->gtOper == GT_UDIV)
{
// Only one possible exception
// (AnyVal / 0) => DivideByZeroException
//
// Note that division by the constant 0 was already checked for above by the
// op2->IsIntegralConst(0) check
//
if (!divisorOp->IsCnsIntOrI())
{
// divisorOp is not a constant, so it could be zero
//
emit->emitIns_R_I(INS_cmp, size, divisorReg, 0);
genJumpToThrowHlpBlk(EJ_eq, SCK_DIV_BY_ZERO);
}
genCodeForBinary(tree);
}
}
Expand Down Expand Up @@ -10213,4 +10190,22 @@ void CodeGen::genCodeForCond(GenTreeOp* tree)
genProduceReg(tree);
}

//------------------------------------------------------------------------
// genCodeForChkDivByZero: Generates the code for checking
// if a value is zero to throw a div-by-zero exception.
//
// Arguments:
// tree - divide-by-zero check
//
void CodeGen::genCodeForChkDivByZero(GenTreeOp* tree)
{
assert(tree->OperIs(GT_CHK_DIV_BY_ZERO));
genConsumeOperands(tree->AsOp());

GenTree* op1 = tree->gtGetOp1();

GetEmitter()->emitIns_R_I(INS_cmp, emitActualTypeSize(op1->TypeGet()), op1->GetRegNum(), 0);
genJumpToThrowHlpBlk(EJ_eq, SCK_DIV_BY_ZERO);
}

#endif // TARGET_ARM64
4 changes: 4 additions & 0 deletions src/coreclr/jit/codegenarmarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ void CodeGen::genCodeForTreeNode(GenTree* treeNode)
break;

#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
genCodeForChkDivByZero(treeNode->AsOp());
break;

case GT_INC_SATURATE:
genCodeForIncSaturate(treeNode);
break;
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10727,6 +10727,9 @@ class GenTreeVisitor
case GT_ARR_ADDR:
case GT_KEEPALIVE:
case GT_INC_SATURATE:
#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
#endif
{
GenTreeUnOp* const unOp = node->AsUnOp();
if (unOp->gtOp1 != nullptr)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4271,6 +4271,9 @@ void GenTree::VisitOperands(TVisitor visitor)
case GT_RETURNTRAP:
case GT_KEEPALIVE:
case GT_INC_SATURATE:
#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
#endif
visitor(this->AsUnOp()->gtOp1);
return;

Expand Down
23 changes: 23 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5099,6 +5099,13 @@ unsigned Compiler::gtSetEvalOrder(GenTree* tree)
costSz = 7; // jump to cold section
break;

#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
costEx = 4;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the costs should be. I just copied from GT_BOUNDS_CHECK from above.

costSz = 7;
break;
#endif

case GT_COMMA:

/* Comma tosses the result of the left operand */
Expand Down Expand Up @@ -5895,6 +5902,9 @@ bool GenTree::TryGetUse(GenTree* operand, GenTree*** pUse)
case GT_BSWAP16:
case GT_KEEPALIVE:
case GT_INC_SATURATE:
#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
#endif
if (operand == this->AsUnOp()->gtOp1)
{
*pUse = &this->AsUnOp()->gtOp1;
Expand Down Expand Up @@ -6402,6 +6412,9 @@ bool GenTree::OperMayThrow(Compiler* comp)
case GT_CKFINITE:
case GT_INDEX:
case GT_INDEX_ADDR:
#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
#endif
return true;

#ifdef FEATURE_HW_INTRINSICS
Expand Down Expand Up @@ -8340,6 +8353,13 @@ GenTree* Compiler::gtCloneExpr(
copy->AsBoundsChk()->gtInxType = tree->AsBoundsChk()->gtInxType;
break;

#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
copy = new (this, GT_CHK_DIV_BY_ZERO)
GenTreeOp(GT_CHK_DIV_BY_ZERO, tree->TypeGet(), tree->AsOp()->gtOp1, nullptr);
break;
#endif

case GT_LEA:
{
GenTreeAddrMode* addrModeOp = tree->AsAddrMode();
Expand Down Expand Up @@ -9120,6 +9140,9 @@ GenTreeUseEdgeIterator::GenTreeUseEdgeIterator(GenTree* node)
case GT_PUTARG_SPLIT:
#endif // FEATURE_ARG_SPLIT
case GT_RETURNTRAP:
#if TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
#endif
m_edge = &m_node->AsUnOp()->gtOp1;
assert(*m_edge != nullptr);
m_advance = &GenTreeUseEdgeIterator::Terminate;
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/gtlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ GTNODE(ADDR , GenTreeOp ,0,GTK_UNOP|DBK_NOTLIR) // addre

GTNODE(BOUNDS_CHECK , GenTreeBoundsChk ,0,GTK_BINOP|GTK_EXOP|GTK_NOVALUE) // a bounds check - for arrays/spans/SIMDs/HWINTRINSICs

#ifdef TARGET_ARM64
GTNODE(CHK_DIV_BY_ZERO , GenTreeOp ,0,GTK_UNOP|GTK_NOVALUE) // a divide-by-zero check - for integers
#endif

GTNODE(IND , GenTreeIndir ,0,GTK_UNOP) // Load indirection
GTNODE(STOREIND , GenTreeStoreInd ,0,GTK_BINOP|GTK_NOVALUE) // Store indirection
GTNODE(OBJ , GenTreeObj ,0,GTK_UNOP|GTK_EXOP) // Object that MAY have gc pointers, and thus includes the relevant gc layout info.
Expand Down
18 changes: 18 additions & 0 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14025,6 +14025,24 @@ void Compiler::impImportBlockCode(BasicBlock* block)
}
}

#if defined(TARGET_ARM64)
if ((oper == GT_DIV || oper == GT_UDIV || oper == GT_MOD || oper == GT_UMOD) &&
!varTypeIsFloating(type) && (!op2->IsIntegralConst() || op2->IsIntegralConst(0)))
{
unsigned tmpNum = lvaGrabTemp(true DEBUGARG("divisor expr"));
GenTree* divisorAsg = gtNewTempAssign(tmpNum, op2);
GenTree* divisor = gtNewLclvNode(tmpNum, op2->TypeGet());
GenTree* chkDivByZero =
new (this, GT_CHK_DIV_BY_ZERO) GenTreeOp(GT_CHK_DIV_BY_ZERO, TYP_VOID, divisor, nullptr);

chkDivByZero->gtFlags |= GTF_SIDE_EFFECT;

op2 =
gtNewOperNode(GT_COMMA, divisor->TypeGet(), divisorAsg,
gtNewOperNode(GT_COMMA, divisor->TypeGet(), chkDivByZero, gtCloneExpr(divisor)));
}
#endif

// We can generate a TYP_FLOAT operation that has a TYP_DOUBLE operand
//
if (varTypeIsFloating(type) && varTypeIsFloating(op1->gtType) && varTypeIsFloating(op2->gtType))
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/liveness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,9 @@ void Compiler::fgComputeLifeLIR(VARSET_TP& life, BasicBlock* block, VARSET_VALAR
case GT_JMP:
case GT_STOREIND:
case GT_BOUNDS_CHECK:
#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
#endif
case GT_STORE_OBJ:
case GT_STORE_BLK:
case GT_STORE_DYN_BLK:
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11538,6 +11538,13 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
fgSetRngChkTarget(tree);
break;

#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
assert(!varTypeIsFloating(op1));
fgAddCodeRef(compCurBB, bbThrowIndex(compCurBB), SCK_DIV_BY_ZERO);
break;
#endif

case GT_OBJ:
case GT_BLK:
case GT_IND:
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10795,6 +10795,12 @@ void Compiler::fgValueNumberAddExceptionSet(GenTree* tree)
fgValueNumberAddExceptionSetForBoundsCheck(tree);
break;

#ifdef TARGET_ARM64
case GT_CHK_DIV_BY_ZERO:
// TODO: Add impl
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to add an impl for the case here.

#endif

case GT_LCLHEAP:
// It is not necessary to model the StackOverflow exception for GT_LCLHEAP
break;
Expand Down