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

Code before:
            case VNF_TrailingZeroCount:
            case VNF_PopCount:
                // We can be a bit more precise here if we want to
                result.lLimit = Limit(Limit::keConstant, 0);
                result.uLimit = Limit(Limit::keConstant, 64);
                break;

            default:
                break;
        }
    }

    MergeEdgeAssertions(comp, num, ValueNumStore::NoVN, assertions, &result, false);
    assert(result.IsConstantRange());
    *pRange = result;
    return true;
}

//------------------------------------------------------------------------
// MergeEdgeAssertions: Merge assertions on the edge flowing into the block about a variable
//
// Arguments:
//    comp                - the compiler instance
//    normalLclVN         - the value number to look for assertions for
//    preferredBoundVN    - when this VN is set, it will be given preference over constant limits
//    assertions          - the assertions to use
//    pRange              - the range to tighten with assertions
//    canUseCheckedBounds - true if we can use checked bounds assertions (cache)
//
void RangeCheck::MergeEdgeAssertions(Compiler*        comp,
                                     ValueNum         normalLclVN,
                                     ValueNum         preferredBoundVN,
                                     ASSERT_VALARG_TP assertions,
                                     Range*           pRange,
                                     bool             canUseCheckedBounds)
{
    Range assertedRange = Range(Limit(Limit::keUnknown));
    if (BitVecOps::IsEmpty(comp->apTraits, assertions))
    {
        return;
    }

    if (normalLclVN == ValueNumStore::NoVN)
    {
        return;
    }

    // Walk through the "assertions" to check if they apply.
    BitVecOps::Iter iter(comp->apTraits, assertions);
    unsigned        index = 0;
    while (iter.NextElem(&index))

Review comment (refactor):
Consider extracting the lambda above the `if` in a follow-up, this looks pretty gnarly

Code after:
                // We can be a bit more precise here if we want to
                result.lLimit = Limit(Limit::keConstant, 0);
                result.uLimit = Limit(Limit::keConstant, 64);
                break;

            default:
                break;
        }
    }

    Range phiRange = Range(Limit(Limit::keUndef));
    if (comp->optVisitReachingAssertions(num,
                                         [comp, &phiRange, &budget](ValueNum reachingVN, ASSERT_TP reachingAssertions) {
        // call GetRangeFromAssertions for each reaching VN using reachingAssertions
        Range edgeRange = GetRangeFromAssertions(comp, reachingVN, reachingAssertions, --budget);

        // If phiRange is not yet set, set it to the first edgeRange
        // else merge it with the new edgeRange. Example: [10..100] U [50..150] = [10..150]
        phiRange = phiRange.IsUndef() ? edgeRange : RangeOps::Merge(phiRange, edgeRange, false);

        // if any edge produces a non-constant range, we abort further processing
        // We also give up if the range is full, as it won't help tighten the result.
        return edgeRange.IsConstantRange() && !edgeRange.IsFullRange() ? Compiler::AssertVisit::Continue
                                                                       : Compiler::AssertVisit::Abort;
    }) == Compiler::AssertVisit::Continue &&
        !phiRange.IsUndef())
    {
        assert(phiRange.IsConstantRange());
        result = phiRange;
    }

    MergeEdgeAssertions(comp, num, ValueNumStore::NoVN, assertions, &result, false);
    assert(result.IsConstantRange());
    return result;
}

//------------------------------------------------------------------------
// MergeEdgeAssertions: Merge assertions on the edge flowing into the block about a variable
//
// Arguments:
//    comp                - the compiler instance
//    normalLclVN         - the value number to look for assertions for
//    preferredBoundVN    - when this VN is set, it will be given preference over constant limits
//    assertions          - the assertions to use
//    pRange              - the range to tighten with assertions
//    canUseCheckedBounds - true if we can use checked bounds assertions (cache)
//
void RangeCheck::MergeEdgeAssertions(Compiler*        comp,
                                     ValueNum         normalLclVN,
                                     ValueNum         preferredBoundVN,
                                     ASSERT_VALARG_TP assertions,