Skip to content

JIT: Optimize simple arithmetic with GT_NEG #13837

@EgorBo

Description

@EgorBo

I was surprised it's not handled in morph, e.g. -a + b => b - a.

int M1(int a, int b) => -a + b; // optimize to "b - a" int M2(int a, int b) => a + -b; // optimize to "a - b" int M3(int a, int b) => a - -b; // optimize to "a + b" int M4(int a, int b) => -a - -b; // optimize to "b - a"

Current codegen:

; Method CC:M1(int,int):int:this G_M48868_IG02:  mov eax, edx  neg eax  add eax, r8d ; Method CC:M2(int,int):int:this G_M15271_IG02:  mov eax, r8d  neg eax  add eax, edx ; Method CC:M3(int,int):int:this G_M13857_IG02:  mov eax, r8d  neg eax  sub edx, eax  mov eax, edx ; Method CC:M4(int,int):int:this G_M28384_IG02:  mov eax, edx  neg eax  mov edx, r8d  neg edx  sub eax, edx

Expected codegen: https://godbolt.org/z/nmZxv8

Also:

int M5(int a) => -a / 10; // optimize to "a / -10" int M6(int a) => -(a / 10); // optimize to "a / -10" int M7(int a) => -a * 10; // optimize to "a * -10" int M8(int a) => -(a * 10); // optimize to "a * -10"

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

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions