Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unnecessary sign-extension for (nuint)span.Length #88136

Merged
merged 19 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ bool IntegralRange::Contains(int64_t value) const
}
break;

case GT_IND:
{
#if defined(_WIN64)
if (node->gtGetOp1()->OperGet() == GT_ADD
&& node->gtGetOp1()->gtGetOp1()->OperGet() == GT_LCL_VAR
&& node->gtGetOp1()->gtGetOp2()->IsIntegralConst(OFFSETOF__CORINFO_Span__length))
{
GenTreeLclVar* const lclVar = node->gtGetOp1()->gtGetOp1()->AsLclVar();
if (lclVar->IsSpan(compiler))
{
// TODO: this check can be moved inside isSpan?
assert(compiler->lvaIsImplicitByRefLocal(lclVar->GetLclNum()));
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
}
#endif // _WIN64
break;
}

case GT_LCL_VAR:
{
LclVarDsc* const varDsc = compiler->lvaGetDesc(node->AsLclVar());
Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ class LclVarDsc
private:
unsigned char lvIsNeverNegative : 1; // The local is known to be never negative

unsigned char lvIsSpan : 1; // The local is a Span<T>

public:
union {
unsigned lvFieldLclStart; // The index of the local var representing the first field in the promoted struct
Expand Down Expand Up @@ -956,6 +958,18 @@ class LclVarDsc
lvIsNeverNegative = value;
}

// Is this is local a Span<T>?
bool IsSpan() const
{
return lvIsSpan;
}

// Is this is local a Span<T>?
void SetIsSpan(bool value)
{
lvIsSpan = value;
}

/////////////////////

regNumber GetArgInitReg() const
Expand Down
15 changes: 15 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26020,6 +26020,21 @@ bool GenTreeLclVar::IsNeverNegative(Compiler* comp) const
return comp->lvaGetDesc(GetLclNum())->IsNeverNegative();
}

//------------------------------------------------------------------------
// GenTreeLclVar::IsSpan: Gets true if the lcl var is a Span<T>; otherwise false.
//
// Arguments:
// comp - the compiler instance
//
// Return Value:
// true if the lcl var is a Span<T>; otherwise false.
//
bool GenTreeLclVar::IsSpan(Compiler* comp) const
{
assert(OperIs(GT_LCL_VAR));
return comp->lvaGetDesc(GetLclNum())->IsSpan();
}

#if defined(TARGET_XARCH) && defined(FEATURE_HW_INTRINSICS)
//------------------------------------------------------------------------
// GetResultOpNumForRmwIntrinsic: check if the result is written into one of the operands.
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,8 @@ struct GenTreeLclVar : public GenTreeLclVarCommon

bool IsNeverNegative(Compiler* comp) const;

bool IsSpan(Compiler* comp) const;

//-------------------------------------------------------------------
// clearOtherRegFlags: clear GTF_* flags associated with gtOtherRegs
//
Expand Down
19 changes: 13 additions & 6 deletions src/coreclr/jit/lclmorph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,13 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>

if (addr->IsLclVarAddr())
{
const LclVarDsc* varDsc = m_compiler->lvaGetDesc(addr->AsLclVarCommon());
LclVarDsc* varDsc = m_compiler->lvaGetDesc(addr->AsLclVarCommon());

// addr is a span field so this lclVar must be a span
if (isSpanLength)
{
varDsc->SetIsSpan(true);
}

if (varDsc->lvPromoted)
{
Expand All @@ -1256,18 +1262,19 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>

LclVarDsc* fieldVarDsc = m_compiler->lvaGetDesc(fieldLclNum);

// Span's Length is never negative unconditionally
if (isSpanLength && (accessSize == genTypeSize(TYP_INT)))
{
fieldVarDsc->SetIsNeverNegative(true);
}

// Retargeting the indirection to reference the promoted field would make it "wide", exposing
// the whole parent struct (with all of its fields).
if (accessSize > genTypeSize(fieldVarDsc))
{
return BAD_VAR_NUM;
}

if (isSpanLength && (accessSize == genTypeSize(TYP_INT)))
{
fieldVarDsc->SetIsNeverNegative(true);
}

JITDUMP("Replacing the field in promoted struct with local var V%02u\n", fieldLclNum);
m_stmtModified = true;

Expand Down