Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/coreclr/jit/llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ class Llvm

Value* consumeAddressAndEmitNullCheck(GenTreeIndir* indir);
void emitNullCheckForAddress(GenTree* addr, Value* addrValue DEBUGARG(GenTree* indir));
bool isAddressNullOrValid(GenTree* addr);
void emitAlignmentCheckForAddress(GenTree* addr, Value* addrValue, unsigned alignment DEBUGARG(GenTree* indir));
bool isAddressAligned(GenTree* addr, unsigned alignment);

Expand Down
30 changes: 27 additions & 3 deletions src/coreclr/jit/llvmcodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2340,10 +2340,10 @@ void Llvm::emitNullCheckForAddress(GenTree* addr, Value* addrValue DEBUGARG(GenT
{
// The frontend's contract with the backend is that it will not insert null checks for accesses which
// are inside the "[0..compMaxUncheckedOffsetForNullObject]" range. Thus, we usually need to check not
// just for "null", but "null + small offset". However, for TYP_REF, we know it will either be a valid
// object on heap, or null, and can utilize the more direct form.
// just for "null", but "null + small offset". However, for certain addresses, we know it will either
// be a valid address, or null.
Value* isNullValue;
if (addr->TypeIs(TYP_REF))
if (isAddressNullOrValid(addr))
{
// LLVM's FastISel, used for unoptimized code, is not able to generate sensible WASM unless we do
// a comparison using an integer zero here. This workaround saves 5+% on debug code size.
Expand All @@ -2368,6 +2368,30 @@ void Llvm::emitNullCheckForAddress(GenTree* addr, Value* addrValue DEBUGARG(GenT
emitJumpToThrowHelper(isNullValue, CORINFO_HELP_THROWNULLREF DEBUGARG(indir));
}

bool Llvm::isAddressNullOrValid(GenTree* addr)
{
if (addr->TypeIs(TYP_REF))
{
return true;
}

// Weed out transient byrefs that could've been created by "fgMorphField". This is not as easy as it may look
// as we must accomodate for the possibility of the frontend transforming these in arbitrary ways. We do this
// by taking advantage of the managed ABI which prohibits passing such byrefs across call boundaries.
if (addr->OperIs(GT_LCL_VAR) && (addr->AsLclVar()->GetSsaNum() == SsaConfig::FIRST_SSA_NUM) &&
_compiler->lvaGetDesc(addr->AsLclVar())->lvIsParam)
{
return true;
}

if (addr->IsCall())
{
return true;
}

return false;
}

void Llvm::emitAlignmentCheckForAddress(GenTree* addr, Value* addrValue, unsigned alignment DEBUGARG(GenTree* indir))
{
if (isAddressAligned(addr, alignment))
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/llvmlower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ void Llvm::lowerAddressToAddressMode(GenTreeIndir* indir)
(size_t)offset);

// Invariant access can be assumed to be in bounds by construction.
if (((indir->gtFlags & GTF_IND_INVARIANT) == 0) && !isAddressInBounds(baseAddr, fieldSeq, offset))
if (!indir->IsInvariantLoad() && !isAddressInBounds(baseAddr, fieldSeq, offset))
{
JITDUMP("no, not in bounds\n");
return;
Expand Down
Loading