Skip to content

JIT does not eliminate null check for "o ??= new()" #75987

@tfenise

Description

@tfenise

Description

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIGYACMhgYQYG8aHunHjykTFAwCy5ABQRgAKxhgMDCAEoOXHuoCWAMwbjJDAPwGAvAwB2MAO6KZcjOKUqNucwFcANu5UYAFlAjWFtYAoghgMAAOGBoQZg4A3GrcAL5JDGn0TAJCoqSStvKKKpzU6po6ehAMpkE2svIOTi5mHl4Mvv6BVgyh4VExcUqJpTypI9wZfNnEwiJ04hpmChrFaeW6GgwAPAwADE7Ve8NlPNob23vefgHm3b2R0bEJaWPqk1mCM6Io+fUKyqpxgwxskgA=

static void M1(object o) {
    if ((o ??= new object()) is null) throw new Exception();
}
    
static void M2(object o) {
    if ((o = new object()) is null) throw new Exception();
}

static void M3(int i) {
    if (i < 0) i = 0;
    if (i < 0) throw new Exception();
}
    
static void M4(object o) {
}

While the JIT can optimize M2(almost, but still curiously worse than M4) and M3 to a nop, it cannot optimize M1 similarly.

Found while discussing #75936.

Configuration

sharplab says "Core CLR 6.0.822.36306 on amd64".

I also tried 7.0.100-rc.1.22431.12 on amd64. The codegen for M1 was not really better.

Regression?

Probably not.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions