diff --git a/src/coreclr/jit/flowgraph.cpp b/src/coreclr/jit/flowgraph.cpp index 8c0c4daf4acf5..cbdabfd930c7e 100644 --- a/src/coreclr/jit/flowgraph.cpp +++ b/src/coreclr/jit/flowgraph.cpp @@ -1151,14 +1151,26 @@ bool Compiler::fgCastNeeded(GenTree* tree, var_types toType) // // Is the tree as GT_CAST or a GT_CALL ? // - if (tree->OperGet() == GT_CAST) + if (tree->OperIs(GT_CAST)) { fromType = tree->CastToType(); } - else if (tree->OperGet() == GT_CALL) + else if (tree->OperIs(GT_CALL)) { fromType = (var_types)tree->AsCall()->gtReturnType; } + else if (tree->OperIs(GT_LCL_VAR)) + { + LclVarDsc* varDsc = lvaGetDesc(tree->AsLclVarCommon()); + if (varDsc->lvNormalizeOnStore()) + { + fromType = varDsc->TypeGet(); + } + else + { + fromType = tree->TypeGet(); + } + } else { fromType = tree->TypeGet(); diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index f24750613054a..2238924ab064a 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -1406,11 +1406,19 @@ var_types Compiler::impImportCall(OPCODE opcode, // Such form allows to find statements with fat calls without walking through whole trees // and removes problems with cutting trees. assert(IsTargetAbi(CORINFO_NATIVEAOT_ABI)); - if (call->OperGet() != GT_LCL_VAR) // can be already converted by impFixupCallStructReturn. + if (!call->OperIs(GT_LCL_VAR)) // can be already converted by impFixupCallStructReturn. { unsigned calliSlot = lvaGrabTemp(true DEBUGARG("calli")); LclVarDsc* varDsc = lvaGetDesc(calliSlot); + // Keep the information about small typedness to avoid + // inserting unnecessary casts around normalization. + if (call->IsCall() && varTypeIsSmall(call->AsCall()->gtReturnType)) + { + assert(call->AsCall()->NormalizesSmallTypesOnReturn()); + varDsc->lvType = call->AsCall()->gtReturnType; + } + // TODO-Bug: CHECK_SPILL_NONE here looks wrong. impStoreTemp(calliSlot, call, CHECK_SPILL_NONE); // impStoreTemp can change src arg list and return type for call that returns struct. var_types type = genActualType(lvaTable[calliSlot].TypeGet());