Skip to content

JIT: (bug) PHI-based jump threading leaves stale VNs on the parents of rewritten PHI uses, so CSE reuses a value from a different SSA def #135813

Description

@EgorBo

After SSA-aware PHI jump threading, optJumpThreadCore updates the VN of the rewritten LCL_VAR use but not of its parents. Trees that read different SSA defs of a loop-carried variable end up with identical VNs, and Optimize Valnum CSEs reuses a value from the wrong def, producing a wrong result.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int v0, int v1, int v2, int a, int c)
    {
        int flag = 0;
        int x = v0;
        int k = a;
        while (true)
        {
            if (flag == 0)
            {
                if (x * a == c)
                    return -1;
            }
            else
            {
                int z = x * a + k;
                if (z > c)
                    return z;
            }
            flag = 1;
            x = v1;
            k = v2;
        }
    }

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

Expected

1035 (second iteration: 5 * 7 + 1000)

Actual

1021 (= v0 * a + v2: the first iteration's x * a is reused in the second one). The loop body disassembles to v2 + (v0 * a) and v1 is unused.

Regression?

Yes, a regression in .NET 11: 8.0.x, 9.0.x and 10.0.12 print 1035; 11.0.0-rc.1 and current main print 1021. Likely introduced by #126812 (SSA-aware PHI jump threading, re-enabled in #127103).

Notes

  • DOTNET_JitDoRedundantBranchOpts=0 and DOTNET_JITMinOpts=1 both print 1035.
  • JitDump: Updating [000038] VN from $102 {PhiDef(V07 d:3, u:4, u:2)} to $80 {InitVal($40)}. Only the LCL_VAR leaf is updated; its MUL/ADD parents keep VNs derived from PhiDef(x.3) ($141/$146 in both blocks).
  • The fix-up in optJumpThreadCore (redundantbranchopts.cpp, after use->SetVNs(newVNPair)) only walks GT_COMMA parents. All ancestors need renumbering/invalidation, or CSE must be blocked for rewritten trees.

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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. self-assigned this
    on Nov 20, 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