Skip to content
Open
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
85 changes: 72 additions & 13 deletions src/coreclr/jit/rangecheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,17 @@ struct RangeOps
return Limit(Limit::keUnknown);
}

// Even when the unsigned overflow check above passes, the signed multiplications
// below can still overflow (which is undefined behavior on signed int). Re-check
// with signed semantics to keep the int min/max computation well-defined.
if (unsignedMul && (CheckedOps::MulOverflows(r1lo, r2lo, CheckedOps::Signed) ||
CheckedOps::MulOverflows(r1lo, r2hi, CheckedOps::Signed) ||
CheckedOps::MulOverflows(r1hi, r2lo, CheckedOps::Signed) ||
CheckedOps::MulOverflows(r1hi, r2hi, CheckedOps::Signed)))
{
return Limit(Limit::keUnknown);
}

int lo = min(min(r1lo * r2lo, r1lo * r2hi), min(r1hi * r2lo, r1hi * r2hi));
int hi = max(max(r1lo * r2lo, r1lo * r2hi), max(r1hi * r2lo, r1hi * r2hi));
assert(hi >= lo);
Expand Down Expand Up @@ -431,21 +442,67 @@ struct RangeOps
return Multiply(r1, convertedOp2Range);
}

// Compute a safe upper bound for the bitwise OR / XOR of two non-negative
// ranges with upper bounds 'b' and 'd': the smallest 2^k - 1 mask that is
// >= max(b, d). Bits in the result can be at most as high as the top bit
// of max(b, d).
//
// NOTE: max(b, d) must be non-negative (i.e., fit in a 31-bit signed int).
//
Comment thread
EgorBo marked this conversation as resolved.
static int BitwiseUpperBoundForNonNegative(int b, int d)
{
unsigned m = static_cast<unsigned>(max(b, d));
m |= m >> 1;
m |= m >> 2;
m |= m >> 4;
m |= m >> 8;
m |= m >> 16;
// m is now of the form 2^k - 1 with m >= max(b, d). Since max(b, d) <= INT32_MAX,
// the result m <= INT32_MAX, so the cast back to signed is safe.
return static_cast<int>(m);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just noting that I am already adding a similar function here. I already use it for the existing sub->xor transform in morph.
In particular BitsetFromRange(0, max(b, d)) has the same effect.

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.

@BoyBaykiller yeah, my copilot even tried to add XOR along the way there. I'll take a look at your PR tomorrow, I'm not sure I like the XOR impl


static Range Or(const Range& r1, const Range& r2)
{
// For OR we require both operands to be constant to produce a constant result.
// No useful information can be derived if only one operand is constant.
int r1ConstVal;
int r2ConstVal;
bool r1IsConstVal = r1.IsSingleValueConstant(&r1ConstVal);
bool r2IsConstVal = r2.IsSingleValueConstant(&r2ConstVal);

// Both single constants - compute the exact result.
if (r1IsConstVal && r2IsConstVal)
{
return Range(Limit(Limit::keConstant, r1ConstVal | r2ConstVal));
Comment thread
EgorBo marked this conversation as resolved.
}
Comment thread
EgorBo marked this conversation as resolved.

if (!r1.IsConstantRange() || !r2.IsConstantRange())
{
return Range(Limit(Limit::keUnknown));
}
Comment thread
EgorBo marked this conversation as resolved.

int a = r1.LowerLimit().GetConstant();
int b = r1.UpperLimit().GetConstant();
int c = r2.LowerLimit().GetConstant();
int d = r2.UpperLimit().GetConstant();

// Signed cases get hairy.
if ((a < 0) || (c < 0))
{
return Range(Limit(Limit::keUnknown));
}

// For non-negative ranges:
// Lower bound: x | y >= max(x, y), so result >= max(a, c).
// Upper bound: x | y can have at most as many bits set as the highest bit of
// max(b, d). Conservatively round up to the next "all ones" mask.
//
// Example: [0..3] | [1..255] = [1..255]
// [X..Y] | [1..255] = [unknown..unknown]
// NOTE: a naive `b | d` is unsound here. For example, [3..4] | [4..4] would
// give upper = 4|4 = 4, but 3|4 = 7 is also reachable.
//
return ApplyRangeOp(r1, r2, [](const Limit& a, const Limit& b) {
if (a.IsConstant() && b.IsConstant() && (a.GetConstant() >= 0) && (b.GetConstant() >= 0))
{
return Limit(Limit::keConstant, a.GetConstant() | b.GetConstant());
}
return Limit(Limit::keUnknown);
});
// Example: [3..5] | [4..7] = [max(3,4) .. mask(max(5,7))] = [4..7]
//
return Range(Limit(Limit::keConstant, max(a, c)),
Limit(Limit::keConstant, BitwiseUpperBoundForNonNegative(b, d)));
Comment thread
EgorBo marked this conversation as resolved.
}

static Range And(const Range& r1, const Range& r2)
Expand Down Expand Up @@ -623,10 +680,12 @@ struct RangeOps
return Limit(Limit::keUnknown);
}

// Keep it simple for now, check if 0 <= C < 31
// Allow shift counts in [1..30]. Shift count 31 can produce implementation-defined
// codegen or UB for `1 << 31`, and the resulting INT_MIN cannot be used safely by
// the subsequent Multiply.
int r1loConstant = r1lo.GetConstant();
int r1hiConstant = r1hi.GetConstant();
if (r1loConstant <= 0 || r1loConstant > 31 || r1hiConstant <= 0 || r1hiConstant > 31)
if ((r1loConstant <= 0) || (r1loConstant > 30) || (r1hiConstant <= 0) || (r1hiConstant > 30))
{
return Limit(Limit::keUnknown);
}
Expand Down
Loading