[x86-64] Conditional increment on non-carry non-overflow => speculative increment+cmov
#include <stdint.h>
uint64_t foo(uint64_t a, uint64_t b) {
return a + ((b & 63) != 0);
}
Compiles to:
foo:
and esi, 63
mov rax, rdi
cmp rsi, 1
sbb rax, -1
ret
Could be:
foo:
lea rax, [rdi + 1] ; speculative a + 1
test esi, 63 ; ZF = 1 iff (idx & 63) == 0 (non-destructive)
cmovz rax, rdi ; if zero, take the un-incremented a
ret
@llvm/issue-subscribers-backend-x86
Author: Niles Salter (Validark)
#include <stdint.h>
uint64_t foo(uint64_t a, uint64_t b) {
return a + ((b & 63) != 0);
}
Compiles to:
foo:
and esi, 63
mov rax, rdi
cmp rsi, 1
sbb rax, -1
ret
Could be:
foo:
lea rax, [rdi + 1] ; speculative a + 1
test esi, 63 ; ZF = 1 iff (idx & 63) == 0 (non-destructive)
cmovz rax, rdi ; if zero, take the un-incremented a
ret
define i64 @foo(i64 noundef %a, i64 noundef %b) {
%and = and i64 %b, 63
%cmp = icmp ne i64 %and, 0
%conv = zext i1 %cmp to i64
%add = add i64 %a, %conv
ret i64 %add
}
Can I work on this?
Can I work on this?
Sure, but I think the first thing we should do is confirm that its actually more performant - the instruction saving is minimal and cmov has a tendency to stall - do you have access to a variety of x86 boxes to compare perf?
https://c.godbo.lt/z/13ebcveTM (APX)
Iterations: 100
Instructions: 400
Total Cycles: 110
Total uOps: 500
Dispatch Width: 6
uOps Per Cycle: 4.55
IPC: 3.64
Block RThroughput: 1.0
Instruction Info:
[1]: #uOps
[2]: Latency
[3]: RThroughput
[4]: MayLoad
[5]: MayStore
[6]: HasSideEffects (U)
[1] [2] [3] [4] [5] [6] Instructions:
1 1 1.00 lea rax, [rdi + 1]
1 2 0.20 test esi, 63
1 1 0.50 cmove rax, rdi
2 7 0.50 U ret
Iterations: 100
Instructions: 400
Total Cycles: 205
Total uOps: 500
Dispatch Width: 6
uOps Per Cycle: 2.44
IPC: 1.95
Block RThroughput: 1.0
Instruction Info:
[1]: #uOps
[2]: Latency
[3]: RThroughput
[4]: MayLoad
[5]: MayStore
[6]: HasSideEffects (U)
[1] [2] [3] [4] [5] [6] Instructions:
1 2 0.20 and esi, 63
1 1 0.20 cmp rsi, 1
1 1 0.50 sbb rax, rdi, -1
2 7 0.50 U ret
This is a good case of when I'd like to see comparative perf in real life examples tbh and not just rely on llvm-mca, the scheduler models often struggle with some of these nuances.
LEA is tricky given each uarch behaves. Given that, I think we should start modeling the LEAs ops wise to make sure these can be captured in llvm-mca as well.
Can I work on this?
Sure, but I think the first thing we should do is confirm that its actually more performant - the instruction saving is minimal and cmov has a tendency to stall - do you have access to a variety of x86 boxes to compare perf?
Hey I don't have a variety of x86 machines, have an AMD Zen Version Machine.
Can I work on this?
Sure, but I think the first thing we should do is confirm that its actually more performant - the instruction saving is minimal and cmov has a tendency to stall - do you have access to a variety of x86 boxes to compare perf?
Even if cmov stalls on current architectures, I definitely think this has to be a valuable optimization at some point
No arguments there - but this might need to be cost driven in a fixup pass - and that needs decent scheduler modeling
Hey @RKSimon as a first time contributor to Backend I am feeling this to be too difficult for me so unassigning myself, but would be a great learning experience once someones puts the PR. Would like to solve some Good First Issues before the harder issues. Apologies for the delay.