Skip to content
Merged
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
33 changes: 19 additions & 14 deletions src/coreclr/jit/rangecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ void RangeCheck::MergeEdgeAssertionsWorker(Compiler* comp
ValueNumStore::SmallValueNumSet* visited)
{
Range assertedRange = Range(Limit(Limit::keUnknown));
if (BitVecOps::IsEmpty(comp->apTraits, assertions))
if (BitVecOps::MayBeUninit(assertions) || BitVecOps::IsEmpty(comp->apTraits, assertions))
{
return;
}
Expand Down Expand Up @@ -2287,9 +2287,19 @@ Range RangeCheck::ComputeRange(BasicBlock* block, GenTree* expr, bool monIncreas
// If VN is constant return range as constant.
else if (m_compiler->vnStore->IsVNConstant(vn))
{
range = (m_compiler->vnStore->TypeOfVN(vn) == TYP_INT)
? Range(Limit(Limit::keConstant, m_compiler->vnStore->ConstantValue<int>(vn)))
: Limit(Limit::keUnknown);
// We want to handle constants first since it can avoid other more expensive work

int cns;
if (m_compiler->vnStore->IsVNIntegralConstant(vn, &cns))
Comment thread
tannergooding marked this conversation as resolved.
{
range = Range(Limit(Limit::keConstant, cns));
}
else
{
// TODO: We could return `0, keUnknown` if the constant is known positive
// but this would require more handling in other places to take advantage of.
range = Limit(Limit::keUnknown);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can TBH, a never-negative long that doesn't fit into INT32 can be negative when casted to INT32?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t that be handled by the cast?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant I don't see what we can possibly do for a LONG that doesn't fit into INT here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same consideration as for TYP_INT.

Which is that the MSB is never set for this target format and so therefore shifts, mods, and various other operations can be optimized as the unsigned variants.

If we cast from TYP_LONG to TYP_INT and the value is unknown, then the cast result might be negative, in the same way that TYP_INT -> TYP_BYTE might produce a negative result, and so the same optimizations on the cast result are then not possible.

}
}
// If local, find the definition from the def map and evaluate the range for rhs.
else if (expr->IsLocal())
Expand Down Expand Up @@ -2331,20 +2341,10 @@ Range RangeCheck::ComputeRange(BasicBlock* block, GenTree* expr, bool monIncreas
JITDUMP("%s\n", range.ToString(m_compiler));
}
}
else if (varTypeIsSmall(expr))
{
range = GetRangeFromType(expr->TypeGet());
JITDUMP("%s\n", range.ToString(m_compiler));
}
else if (expr->OperIs(GT_COMMA))
{
range = GetRangeWorker(block, expr->gtEffectiveVal(), monIncreasing DEBUGARG(indent + 1));
}
else if (expr->OperIs(GT_CAST))
{
// TODO: consider computing range for CastOp and intersect it with this.
range = GetRangeFromType(expr->AsCast()->CastToType());
}
else if (expr->OperIs(GT_ARR_LENGTH))
{
ValueNum arrLenVN = m_compiler->optConservativeNormalVN(expr);
Expand All @@ -2360,6 +2360,11 @@ Range RangeCheck::ComputeRange(BasicBlock* block, GenTree* expr, bool monIncreas
range = Range(Limit(Limit::keConstant, 0), Limit(Limit::keConstant, CORINFO_Array_MaxLength));
}
}
else if (genActualType(expr) == TYP_INT)
{
// Use GetRangeFromAssertions for everything else since they won't produce dependent or symbolic ranges
range = GetRangeFromAssertions(m_compiler, vn, block->bbAssertionIn);
}
Comment thread
tannergooding marked this conversation as resolved.
else
{
// The expression is not recognized, so the result is unknown.
Expand Down
Loading