Skip to content

[JIT] Reduce FP multiplication by -1 to plain negation #122272

@neon-sunset

Description

@neon-sunset

Description

Given operations like x *= -1, it appears JIT currently does not reduce this operation, using costlier FP multiplication.

Clang/LLVM:

double negate_mul(double num) {
    return num * -1;
}

double negate(double num) {
    return -num;
}
.LCPI0_0:
        .quad   0x8000000000000000
        .quad   0x8000000000000000
negate_mul:
        xorps   xmm0, xmmword ptr [rip + .LCPI0_0]
        ret

.LCPI1_0:
        .quad   0x8000000000000000
        .quad   0x8000000000000000
negate:
        xorps   xmm0, xmmword ptr [rip + .LCPI1_0]
        ret

RyuJIT:

static double NegateMul(double num) => num * -1;
static double Negate(double num) => -num;
// coreclr trunk-20251207+86dfeb91ce270da17416069463e33363bda3ec55

Program:.ctor():this (FullOpts):
       ret      

Program:NegateMul(double):double (FullOpts):
       vmulsd   xmm0, xmm0, qword ptr [reloc @RWD00]
       ret      
RWD00  	dq	BFF0000000000000h	;           -1

Program:Negate(double):double (FullOpts):
       vxorps   xmm0, xmm0, xmmword ptr [reloc @RWD00]
       ret      
RWD00  	dq	8000000000000000h, 8000000000000000h

Metadata

Metadata

Assignees

No one assigned

    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 contributorstenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions