dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.97k stars 4.65k forks source link

JIT: missing opportunities in constant folding around bitwise ops #95747

Open EgorBo opened 9 months ago

EgorBo commented 9 months ago
int Test(int x, int y) => (x | 5) | (y | 3); // E.g. Enums

Current asm:

; Method Program:Test(int,int):int:this (FullOpts)
       or       edx, 5
       mov      eax, edx
       or       eax, r8d
       or       eax, 3
       ret      
; Total bytes of code: 12

Expected asm:

       mov      eax, edx
       or       eax, r8d
       or       eax, 7
       ret      
; Total bytes of code: 9
ghost commented 9 months ago

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

Issue Details
```cs int Test(int x, int y) => (x | 5) | (y | 3); // E.g. Enums ``` Current asm: ```asm ; Method Program:Test(int,int):int:this (FullOpts) or edx, 5 mov eax, edx or eax, r8d or eax, 3 ret ; Total bytes of code: 12 ``` Expected asm: ```asm mov eax, edx or eax, r8d or eax, 7 ret ; Total bytes of code: 9 ```
Author: EgorBo
Assignees: -
Labels: `area-CodeGen-coreclr`, `untriaged`
Milestone: -
AndyAyersMS commented 6 months ago

@EgorBo was this just something you noticed?

99136 shows no diffs, I am trying to figure out if these are just rare, or that PR is not catching some known cases.

tannergooding commented 6 months ago

We tend to manually optimize a lot of the patterns in the libraries code and the tests tend to be simple enough that we don't end up with patterns like this.

So I think there's likely a broad range of "simple" optimizations like this where we'll get next to zero diffs ourselves, but where they may have broader impact to more general .NET code. I expect it still won't be massive number, just wanting to ensure we account for the fact that our own code not having hits isn't necessarily representative of how impactful a change might be.