Skip to content

JIT: Optimize -(-x) to x #13647

@EgorBo

Description

@EgorBo

JIT should be able to eliminate double negations, as far as I understand it's safe to do so for any signed integers and floats:

static float M1(float a)
{
    return -(-a); // e.g. after inlining
}

static int I1(int a)
{
    return -(-a);
}

/*
\--*  NEG       float 
   \--*  NEG       float 
      \--*  LCL_VAR   float  V00 arg0
*/

Current codegen:

; Method M1(float):float
G_M37219_IG01:
       vzeroupper 
G_M37219_IG02:
       vmovss   xmm1, dword ptr [reloc @RWD00]
       vxorps   xmm0, xmm1
       vmovss   xmm1, dword ptr [reloc @RWD00]
       vxorps   xmm0, xmm1
G_M37219_IG03:
       ret      
RWD00  dd	80000000h
; Total bytes of code: 28


; Method I1(int):int
G_M37095_IG01:
G_M37095_IG02:
       mov      eax, ecx
       neg      eax
       neg      eax
G_M37095_IG03:
       ret      
; Total bytes of code: 7

Expected codegen:

; Method M1(float):float
G_M37219_IG01:
       vzeroupper 
G_M37219_IG02:
       ret      
; Total bytes of code: 4


; I1(int):int
G_M37095_IG01:
G_M37095_IG02:
       mov      eax, ecx
G_M37095_IG03:
       ret      
; Total bytes of code: 3

A double negation might confuse the optimization introduced in dotnet/coreclr#27060

category:cq
theme:basic-cq
skill-level:beginner
cost:small

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions