Skip to content

JIT: (bug) IsMonotonicallyIncreasing accepts a negative seed behind lcl + (lcl + cns), relop folded incorrectly #134454

Closed

Description

@EgorBo

i decreases for the first iterations (k = m + 2 starts at -2), but range check treats i += k as monotonically increasing and folds i < -2 to false.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        Console.WriteLine(Test(0));
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int n)
    {
        int i = 0, m = -4;
        while (i < 100 && m < 20)
        {
            if (i < -2)
                return i;
            int k = m + 2;
            i += k;
            n ^= k;
            m++;
        }
        return n;
    }
}

Expected

-3

Actual

14

Regression?

Yes, regressed in .NET 11: .NET 8, 9 and 10 are correct; .NET 11 RC2 and main are affected (win-x64).

Notes

For i + k (lcl + lcl), IsBinOpMonotonicallyIncreasing checks k with rejectNegativeConst=true, but k = m + 2 is lcl + cns, which recurses into m with rejectNegativeConst=false, so m's negative seed -4 is accepted.
The flag should be propagated through the GT_ADD case in IsMonotonicallyIncreasing. The fold happens in assertion prop via RangeCheck::TryGetRange, so DOTNET_JitDoRangeAnalysis=0 does not hide it.
Related to #133818 (different root cause: the alreadyPresent search-path shortcut).

Activity

  1. added this to the 12.0.0 milestone on Sep 22, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 22, 2026
  3. dotnet-policy-service commented on Sep 22, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added a commit that references this issue on Oct 16, 2026
    bc110ba
  5. locked and limited conversation to collaborators on Nov 16, 2026
  6. self-assigned this
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions