Open MineCake147E opened 1 month ago
When I was investigating the codegen of the method:
public static int C(int x, int y) { var g = x > y ? 1 : 0; var l = x < y ? 1 : 0; return g - l; }
The codegen looked like this:
xor eax, eax cmp ecx, edx setg al setl cl movzx ecx, cl sub eax, ecx ret
But when it's called multiple times in a single function, comparison instructions often gets duplicated like:
xor ecx, ecx cmp eax, r8d setg cl xor r14d, r14d cmp eax, r8d setl r14b sub ecx, r14d
cmp eax, r8d seems to be duplicated as xor r14d, r14d modifies the flag register. It should be better relocating the xor r14d, r14d before the cmp eax, r8d, like:
cmp eax, r8d
xor r14d, r14d
xor ecx, ecx xor r14d, r14d cmp eax, r8d setg cl setl r14b sub ecx, r14d
Which should save an ALU executing it.
SharpLab
No
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.
Description
When I was investigating the codegen of the method:
The codegen looked like this:
But when it's called multiple times in a single function, comparison instructions often gets duplicated like:
cmp eax, r8d
seems to be duplicated asxor r14d, r14d
modifies the flag register. It should be better relocating thexor r14d, r14d
before thecmp eax, r8d
, like:Which should save an ALU executing it.
Configuration
SharpLab
Regression?
No
Data
Analysis