KEMBAR78
Signed division performed even when values are provably non-negative · Issue #94218 · dotnet/runtime · GitHub
Skip to content

Signed division performed even when values are provably non-negative #94218

@stephentoub

Description

@stephentoub

Repro:

static int Result(int i)
{
    if (i < 4) return 0;
    return i / 4;
}

This results in:

; Method Program:<<Main>$>g__Result|0_0(int):int (FullOpts)
G_M000_IG01:                ;; offset=0x0000

G_M000_IG02:                ;; offset=0x0000
       cmp      ecx, 4
       jge      SHORT G_M000_IG05

G_M000_IG03:                ;; offset=0x0005
       xor      eax, eax

G_M000_IG04:                ;; offset=0x0007
       ret      

G_M000_IG05:                ;; offset=0x0008
       mov      eax, ecx
       sar      eax, 31
       and      eax, 3
       add      eax, ecx
       sar      eax, 2

G_M000_IG06:                ;; offset=0x0015
       ret      
; Total bytes of code: 22

but as the JIT can prove that the i used in i / 4 isn't negative, so presumably it should be able to emit the code as if:

static int Result(int i)
{
    if (i < 4) return 0;
    return (int)((uint)i / 4);
}

were instead used? And it results in:

; Method Program:<<Main>$>g__Result|0_0(int):int (FullOpts)
G_M000_IG01:                ;; offset=0x0000

G_M000_IG02:                ;; offset=0x0000
       cmp      ecx, 4
       jge      SHORT G_M000_IG05

G_M000_IG03:                ;; offset=0x0005
       xor      eax, eax

G_M000_IG04:                ;; offset=0x0007
       ret      

G_M000_IG05:                ;; offset=0x0008
       mov      eax, ecx
       shr      eax, 2

G_M000_IG06:                ;; offset=0x000D
       ret      
; Total bytes of code: 14

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions