Skip to content

Dead code not eliminated after constant folding/propagation #57033

@stephentoub

Description

@stephentoub

This example is a derivative of one from wikipedia:

[Benchmark]
public int Example()
{
    int a = 30;
    int b = 9 - (a / 5);
    int c;

    c = b * 4;
    if (c > 10)
    {
        c = c - 10;
    }

    return c * (60 / a);
}

The JIT successfully reduces the entire operation to mov eax, 4, but it leaves behind multiple computations that would seem to no longer be necessary:

; .NET 6.0.0 (6.0.21.40103), X64 RyuJIT
; Program.Example()
       mov       ecx,1E
       mov       eax,3C
       xor       edx,edx
       idiv      ecx
       mov       eax,4
       ret
; Total bytes of code 20

Is there some kind of side effect it's relying on from all the earlier gunk, or should it be removing the mov, mov, xor, idiv and just isn't? Note that if I tweak the C# to make int a = 30 be const int a = 30, then all the extra operations go away as expected.

category:cq
theme:value-numbering
skill-level:expert
cost:medium
impact:medium

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIhelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions