Repository: dotnet/runtime
File: src/coreclr/jit/rangecheck.h (C/C++)

Code before:
        // No useful information can be derived if only one operand is constant.
        //
        // Example: [0..3] | [1..255] = [1..255]
        //
        return ApplyRangeOp(r1, r2, [](Limit& a, 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);
        });
    }

    static Range And(Range& r1, Range& r2)
    {
        Limit& r1lo = r1.LowerLimit();
        Limit& r2lo = r2.LowerLimit();

        Limit& r1hi = r1.UpperLimit();
        Limit& r2hi = r2.UpperLimit();

        Range result = Limit(Limit::keUnknown);

        // if either lower bound is a non-negative constant, result >= 0
        if ((r1lo.IsConstant() && (r1lo.GetConstant() >= 0)) || (r2lo.IsConstant() && (r2lo.GetConstant() >= 0)))
        {
            // Example: [0.. ] & [unknown.. ] = [0.. ]
            result.lLimit = Limit(Limit::keConstant, 0);
        }

        // if either upper bound is a non-negative constant, result <= cns
        if (r2hi.IsConstant() && (r2hi.GetConstant() >= 0))
        {
            // Example: [ ..X] & [ ..5] = [ ..5]
            result.uLimit = Limit(Limit::keConstant, r2hi.GetConstant());
        }
        else if (r1hi.IsConstant() && (r1hi.GetConstant() >= 0))
        {
            // Example: [ ..5] & [ ..X] = [ ..5]
            result.uLimit = Limit(Limit::keConstant, r1hi.GetConstant());
        }

        return result;
    }

    static Range UnsignedMod(Range& r1, Range& r2)
    {
        Range result = Limit(Limit::keUnknown);

        Limit& r2lo = r2.LowerLimit();
        Limit& r2hi = r2.UpperLimit();

Review comment (suggestion):
If both are constants you can just compute the limit directly. Is this not worth doing?

Also the comment example might be more thought-provoking if it used something with a nonzero value, eg 

```[4..] & [unknown..] = [0..]```

Code after:
        // 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.
        //
        // Example: [0..3] | [1..255] = [1..255]
        //          [X..Y] | [1..255] = [unknown..unknown]
        //
        return ApplyRangeOp(r1, r2, [](Limit& a, 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);
        });
    }

    static Range And(Range& r1, Range& r2)
    {
        Limit& r1lo = r1.LowerLimit();
        Limit& r2lo = r2.LowerLimit();

        Limit& r1hi = r1.UpperLimit();
        Limit& r2hi = r2.UpperLimit();

        Range result = Limit(Limit::keUnknown);

        // if either lower bound is a non-negative constant, result >= 0
        if ((r1lo.IsConstant() && (r1lo.GetConstant() >= 0)) || (r2lo.IsConstant() && (r2lo.GetConstant() >= 0)))
        {
            // Example: [4.. ] & [unknown.. ] = [4.. ]
            result.lLimit = Limit(Limit::keConstant, 0);
        }
        // Even if both r1 and r2 lower bounds are known non-negative constants, still the best we can do
        // is >= 0 in a general case due to the way bitwise AND works.

        // if either upper bound is a non-negative constant, result <= cns
        if (r2hi.IsConstant() && (r2hi.GetConstant() >= 0))
        {
            // Example: [ ..X] & [ ..5] = [ ..5]
            result.uLimit = Limit(Limit::keConstant, r2hi.GetConstant());
        }
        else if (r1hi.IsConstant() && (r1hi.GetConstant() >= 0))
        {
            // Example: [ ..5] & [ ..X] = [ ..5]
            result.uLimit = Limit(Limit::keConstant, r1hi.GetConstant());
        }

        // Both upper bounds are constant, take the min
        if (r1hi.IsConstant() && r2hi.IsConstant() && (r1hi.GetConstant() >= 0) && (r2hi.GetConstant() >= 0))
        {
            // Example: [ ..7] & [ ..5] = [ ..5]
            result.uLimit = Limit(Limit::keConstant, min(r1hi.GetConstant(), r2hi.GetConstant()));