Skip to content

JIT: (bug) a branch on the bitwise complement of a comparison makes a later identical comparison fold to the wrong value #133557

Closed

Description

@EgorBo

Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int a, int b)
    {
        int t = (a != b) ? 1 : 0;
        int u = ~t;             // -1 or -2, so u is never zero
        if (u != 0)
        {
            if (a != b)
                return 1;
        }
        return 0;
    }

    static void Main() => Console.WriteLine(Test(3, 5));
}

Expected

1

Actual

0

The outer condition ~t != 0 is a tautology that carries no information about a != b, but the
inner comparison is folded from it anyway.

Platform

Windows x64, local Checked build of dotnet/runtime main (ef97eb5). Not target-specific.
MinOpts and release .NET 11.0.100-rc.1.26420.103 both produce the correct 1.

Activity

  1. added this to the 12.0.0 milestone on Sep 10, 2026
  2. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Sep 10, 2026
  3. dotnet-policy-service commented on Sep 10, 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. MihuBot commented on Sep 10, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. self-assigned this
    on Sep 10, 2026
  6. AndyAyersMS commented on Sep 10, 2026

    @AndyAyersMS
    Member

    Bad RBO inferences from GT_NOT. We fixed them in VN, but didn't fix them in RBO. Grr.

  7. added a commit that references this issue on Sep 11, 2026
    9954529
  8. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  9. locked and limited conversation to collaborators on Oct 19, 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-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions