@@ -2340,10 +2340,10 @@ void Llvm::emitNullCheckForAddress(GenTree* addr, Value* addrValue DEBUGARG(GenT
2340
2340
{
2341
2341
// The frontend's contract with the backend is that it will not insert null checks for accesses which
2342
2342
// are inside the "[0..compMaxUncheckedOffsetForNullObject]" range. Thus, we usually need to check not
2343
- // just for "null", but "null + small offset". However, for TYP_REF , we know it will either be a valid
2344
- // object on heap , or null, and can utilize the more direct form .
2343
+ // just for "null", but "null + small offset". However, for certain addresses , we know it will either
2344
+ // be a valid address , or null.
2345
2345
Value* isNullValue;
2346
- if (addr-> TypeIs (TYP_REF ))
2346
+ if (isAddressNullOrValid (addr ))
2347
2347
{
2348
2348
// LLVM's FastISel, used for unoptimized code, is not able to generate sensible WASM unless we do
2349
2349
// a comparison using an integer zero here. This workaround saves 5+% on debug code size.
@@ -2368,6 +2368,30 @@ void Llvm::emitNullCheckForAddress(GenTree* addr, Value* addrValue DEBUGARG(GenT
2368
2368
emitJumpToThrowHelper (isNullValue, CORINFO_HELP_THROWNULLREF DEBUGARG (indir));
2369
2369
}
2370
2370
2371
+ bool Llvm::isAddressNullOrValid (GenTree* addr)
2372
+ {
2373
+ if (addr->TypeIs (TYP_REF))
2374
+ {
2375
+ return true ;
2376
+ }
2377
+
2378
+ // Weed out transient byrefs that could've been created by "fgMorphField". This is not as easy as it may look
2379
+ // as we must accomodate for the possibility of the frontend transforming these in arbitrary ways. We do this
2380
+ // by taking advantage of the managed ABI which prohibits passing such byrefs across call boundaries.
2381
+ if (addr->OperIs (GT_LCL_VAR) && (addr->AsLclVar ()->GetSsaNum () == SsaConfig::FIRST_SSA_NUM) &&
2382
+ _compiler->lvaGetDesc (addr->AsLclVar ())->lvIsParam )
2383
+ {
2384
+ return true ;
2385
+ }
2386
+
2387
+ if (addr->IsCall ())
2388
+ {
2389
+ return true ;
2390
+ }
2391
+
2392
+ return false ;
2393
+ }
2394
+
2371
2395
void Llvm::emitAlignmentCheckForAddress (GenTree* addr, Value* addrValue, unsigned alignment DEBUGARG (GenTree* indir))
2372
2396
{
2373
2397
if (isAddressAligned (addr, alignment))
0 commit comments