Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Final clean up
  • Loading branch information
EgorBo committed Jan 17, 2024
commit b147e67c117493660cd1edcda5e1a7e80c38bba1
24 changes: 13 additions & 11 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4764,17 +4764,14 @@ GenTree* Compiler::optAssertionProp_Call(ASSERT_VALARG_TP assertions, GenTreeCal
{
return optAssertionProp_Update(call, call, stmt);
}
else if (!optLocalAssertionProp && (call->gtCallType == CT_HELPER))
{
if (call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_ISINSTANCEOFINTERFACE) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_ISINSTANCEOFARRAY) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_ISINSTANCEOFCLASS) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_ISINSTANCEOFANY) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_CHKCASTINTERFACE) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_CHKCASTARRAY) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_CHKCASTCLASS) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_CHKCASTANY) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_CHKCASTCLASS_SPECIAL))
else if (!optLocalAssertionProp && call->IsHelperCall())
{
const CorInfoHelpFunc helper = eeGetHelperNum(call->gtCallMethHnd);
if ((helper == CORINFO_HELP_ISINSTANCEOFINTERFACE) || (helper == CORINFO_HELP_ISINSTANCEOFARRAY) ||
(helper == CORINFO_HELP_ISINSTANCEOFCLASS) || (helper == CORINFO_HELP_ISINSTANCEOFANY) ||
(helper == CORINFO_HELP_CHKCASTINTERFACE) || (helper == CORINFO_HELP_CHKCASTARRAY) ||
(helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTANY) ||
(helper == CORINFO_HELP_CHKCASTCLASS_SPECIAL))
{
GenTree* arg1 = call->gtArgs.GetArgByIndex(1)->GetNode();
if (arg1->gtOper != GT_LCL_VAR)
Expand Down Expand Up @@ -4804,6 +4801,11 @@ GenTree* Compiler::optAssertionProp_Call(ASSERT_VALARG_TP assertions, GenTreeCal

return optAssertionProp_Update(arg1, call, stmt);
}

// TODO: check optAssertionIsNonNull for the object argument and replace
// the helper with its nonnull version, e.g.:
// CORINFO_HELP_ISINSTANCEOFANY -> CORINFO_HELP_ISINSTANCEOFANY_NONNULL
// so then fgLateCastExpansion can skip the null check.
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5056,7 +5056,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
DoPhase(this, PHASE_EXPAND_TLS, &Compiler::fgExpandThreadLocalAccess);

// Expand casts
DoPhase(this, PHASE_EXPAND_STATIC_INIT, &Compiler::fgLateCastExpansion);
DoPhase(this, PHASE_EXPAND_CASTS, &Compiler::fgLateCastExpansion);

// Insert GC Polls
DoPhase(this, PHASE_INSERT_GC_POLLS, &Compiler::fgInsertGCPolls);
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compphases.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ CompPhaseNameMacro(PHASE_COMPUTE_EDGE_WEIGHTS2, "Compute edge weights (2, f
CompPhaseNameMacro(PHASE_STRESS_SPLIT_TREE, "Stress gtSplitTree", false, -1, false)
CompPhaseNameMacro(PHASE_EXPAND_RTLOOKUPS, "Expand runtime lookups", false, -1, true)
CompPhaseNameMacro(PHASE_EXPAND_STATIC_INIT, "Expand static init", false, -1, true)
CompPhaseNameMacro(PHASE_EXPAND_CASTS, "Expand casts", false, -1, true)
CompPhaseNameMacro(PHASE_EXPAND_TLS, "Expand TLS access", false, -1, true)
CompPhaseNameMacro(PHASE_INSERT_GC_POLLS, "Insert GC Polls", false, -1, true)
CompPhaseNameMacro(PHASE_CREATE_THROW_HELPERS, "Create throw helper blocks", false, -1, true)
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4080,6 +4080,7 @@ enum GenTreeCallFlags : unsigned int
GTF_CALL_M_HAS_LATE_DEVIRT_INFO = 0x01000000, // this call has late devirtualzation info
GTF_CALL_M_LDVIRTFTN_INTERFACE = 0x02000000, // ldvirtftn on an interface type
GTF_CALL_M_CAST_CAN_BE_EXPANDED = 0x04000000, // this cast (helper call) can be expanded if it's profitable
GTF_CALL_M_CAST_NON_NULL = 0x08000000, // this cast (helper call) can be expanded if it's profitable
};

inline constexpr GenTreeCallFlags operator ~(GenTreeCallFlags a)
Expand Down
35 changes: 33 additions & 2 deletions src/coreclr/jit/helperexpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,25 @@ bool Compiler::fgVNBasedIntrinsicExpansionForCall_ReadUtf8(BasicBlock** pBlock,
return true;
}

//------------------------------------------------------------------------------
// fgLateCastExpansion: Partially inline various cast helpers, e.g.:
//
// tmp = CORINFO_HELP_ISINSTANCEOFINTERFACE(clsHandle, obj);
//
// into:
//
// tmp = obj;
// if ((obj != null) && (obj->pMT != likelyClassHandle))
// {
// tmp = CORINFO_HELP_ISINSTANCEOFINTERFACE(clsHandle, obj);
// }
//
// The goal is to move cast expansion logic from the importer to this phase, for now,
// this phase only supports "isinst" and for profiled casts only.
//
// Returns:
// PhaseStatus indicating what, if anything, was changed.
//
PhaseStatus Compiler::fgLateCastExpansion()
{
if (!doesMethodHaveExpandableCasts())
Expand All @@ -1578,6 +1597,18 @@ PhaseStatus Compiler::fgLateCastExpansion()
return fgExpandHelper<&Compiler::fgLateCastExpansionForCall>(true);
}

//------------------------------------------------------------------------------
// fgLateCastExpansionForCall : Expand specific cast helper, see
// fgLateCastExpansion's comments.
//
// Arguments:
// block - Block containing the cast helper to expand
// stmt - Statement containing the cast helper
// call - The cast helper
//
// Returns:
// True if expanded, false otherwise.
//
bool Compiler::fgLateCastExpansionForCall(BasicBlock** pBlock, Statement* stmt, GenTreeCall* call)
{
if (!call->IsHelperCall() || !impIsCastHelperMayHaveProfileData(call->GetHelperNum()))
Expand Down Expand Up @@ -1774,15 +1805,15 @@ bool Compiler::fgLateCastExpansionForCall(BasicBlock** pBlock, Statement* stmt,
// Wire up the blocks
//
prevBb->SetTarget(nullcheckBb);
nullcheckBb->SetTrueTarget(fallbackBb);
nullcheckBb->SetTrueTarget(block);
nullcheckBb->SetFalseTarget(typeCheckBb);
typeCheckBb->SetTrueTarget(block);
typeCheckBb->SetFalseTarget(fallbackBb);
fallbackBb->SetTarget(block);
fgRemoveRefPred(block, prevBb);
fgAddRefPred(nullcheckBb, prevBb);
fgAddRefPred(typeCheckBb, nullcheckBb);
fgAddRefPred(fallbackBb, nullcheckBb);
fgAddRefPred(block, nullcheckBb);
fgAddRefPred(fallbackBb, typeCheckBb);
fgAddRefPred(block, typeCheckBb);
fgAddRefPred(block, fallbackBb);
Expand Down