diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 9bc7aa13eb59d3..751e15211b57fb 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -10480,16 +10480,26 @@ void Compiler::impImportBlockCode(BasicBlock* block) GenTree* boxPayloadOffset = gtNewIconNode(TARGET_POINTER_SIZE, TYP_I_IMPL); GenTree* boxPayloadAddress = gtNewOperNode(GT_ADD, TYP_BYREF, cloneOperand, boxPayloadOffset); - GenTree* nullcheck = gtNewNullCheck(op1); - // Add an ordering dependency between the null - // check and forming the byref; the JIT assumes - // in many places that the only legal null - // byref is literally 0, and since the byref - // leaks out here, we need to ensure it is - // nullchecked. - nullcheck->SetHasOrderingSideEffect(); - boxPayloadAddress->SetHasOrderingSideEffect(); - GenTree* result = gtNewOperNode(GT_COMMA, TYP_BYREF, nullcheck, boxPayloadAddress); + + GenTree* result; + if (fgAddrCouldBeNull(op1)) + { + GenTree* nullcheck = gtNewNullCheck(op1); + // Add an ordering dependency between the null + // check and forming the byref; the JIT assumes + // in many places that the only legal null + // byref is literally 0, and since the byref + // leaks out here, we need to ensure it is + // nullchecked. + nullcheck->SetHasOrderingSideEffect(); + boxPayloadAddress->SetHasOrderingSideEffect(); + result = gtNewOperNode(GT_COMMA, TYP_BYREF, nullcheck, boxPayloadAddress); + } + else + { + // We don't need a nullcheck if this is e.g. a preinitialized value + result = boxPayloadAddress; + } impPushOnStack(result, tiRetVal); break; }