Skip to content

Commit

Permalink
expand box helper for Nullable<>
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Apr 9, 2021
1 parent 84f97f8 commit a0d8ee9
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6799,9 +6799,37 @@ void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken)
return;
}

GenTreeCall::Use* args =
gtNewCallArgs(op2, impGetStructAddr(exprToBox, operCls, (unsigned)CHECK_SPILL_ALL, true));
op1 = gtNewHelperCallNode(boxHelper, TYP_REF, args);
GenTree* obj = impGetStructAddr(exprToBox, operCls, (unsigned)CHECK_SPILL_ALL, true);
op1 = gtNewHelperCallNode(boxHelper, TYP_REF, gtNewCallArgs(op2, obj));

// In case of Nullable<> let's optimize it to:
//
// if (!o.HasValue)
// null;
// else
// CORINFO_HELP_BOX_NULLABLE(typ, o)
//
if (boxHelper == CORINFO_HELP_BOX_NULLABLE)
{
// Get Nullable<>.hasValue field info
CORINFO_FIELD_HANDLE hasValueFldHnd = info.compCompHnd->getFieldInClass(operCls, 0);
GenTree* clonedObj = gtCloneExpr(obj);
GenTree* hasValueFld = gtNewFieldRef(TYP_BOOL, hasValueFldHnd, clonedObj, 0);

#if DEBUG
assert(info.compCompHnd->getFieldOffset(hasValueFldHnd) == 0);
assert(strcmp(info.compCompHnd->getFieldName(hasValueFldHnd, nullptr), "hasValue") == 0);
#endif

GenTree* cond = gtNewOperNode(GT_NE, TYP_INT, gtNewIconNode(0, TYP_INT), hasValueFld);
GenTree* nullOp = gtNewIconNode(0, TYP_REF);
GenTreeColon* colon = new (this, GT_COLON) GenTreeColon(TYP_REF, op1, nullOp);
GenTreeQmark* qmark = gtNewQmarkNode(TYP_REF, cond, colon);
unsigned tmp = lvaGrabTemp(true DEBUGARG("spilling QMark"));

impAssignTempGen(tmp, qmark, (unsigned)CHECK_SPILL_NONE);
op1 = gtNewLclvNode(tmp, TYP_REF);
}
}

/* Push the result back on the stack, */
Expand Down

0 comments on commit a0d8ee9

Please sign in to comment.