dotnet / runtime

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

Chained comparison of two integers against a constant is not coalesced #101347

Open neon-sunset opened 7 months ago

neon-sunset commented 7 months ago

Description

Given the snippet

static bool Test(int a, int b) => a is 0x80 && b is 0x80;

.NET emits

       cmp      ecx, 128
       jne      SHORT G_M000_IG05
       xor      eax, eax
       cmp      edx, 128
       sete     al
       ret      
G_M000_IG05:                ;; offset=0x0014
       xor      eax, eax
       ret      

While Clang emits

        xor     edi, 128
        xor     esi, 128
        or      esi, edi
        sete    al
        ret

It would be nice if JIT/ILC could emit branchless comparison for such patterns.

Configuration

.NET 8.0.3

Regression?

No

dotnet-policy-service[bot] commented 7 months ago

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