Skip to content

JIT: missing flow optimization pattern #9623

@AndyAyersMS

Description

@AndyAyersMS

See discussion in dotnet/corefx#26087.

using System;
using System.Runtime.CompilerServices;

public class Program
{
    private static int[] _array = new int[3];

    static Program() {}

    public static void Main(string[] args)
    {
        int index = 3;
        MoveNext1(ref index);
        MoveNext2(ref index);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void MoveNext1(ref int index)
    {
        int tmp = index + 1;
        index = (tmp == _array.Length) ? 0 : tmp;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void MoveNext2(ref int index)
    {
        int tmp = index + 1;
        if (tmp == _array.Length) tmp = 0;

        index = tmp;
    }
}

The code for MoveNext1 contains the following branch-around branch sequence:

       394108               cmp      dword ptr [rcx+8], eax
       7402                 je       SHORT G_M38324_IG03
       EB02                 jmp      SHORT G_M38324_IG04

G_M38324_IG03:
       33C0                 xor      eax, eax

G_M38324_IG04:
       8902                 mov      dword ptr [rdx], eax

category:cq
theme:flowgraph
skill-level:expert
cost:small

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions