[x86-64] Conditional increment on non-carry non-overflow => speculative increment+cmov
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
[C Godbolt](https://c.godbo.lt/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:1,endLineNumber:2,positionColumn:1,positionLineNumber:2,selectionStartColumn:1,selectionStartLineNumber:2,startColumn:1,startLineNumber:2),source:'%23include+%3Cstdint.h%3E%0Auint64_t+foo(uint64_t+a,+uint64_t+b)+%7B%0A++++return+a+%2B+((b+%26+63)+!!%3D+0)%3B%0A%7D%0A'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:48.46943717911461,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:cclang_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'-O3+-march%3Dznver5',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),k:51.53056282088541,l:'4',m:100,n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)
```c
#include
uint64_t foo(uint64_t a, uint64_t b) {
return a + ((b & 63) != 0);
}
```
Compiles to:
```asm
foo:
and esi, 63
mov rax, rdi
cmp rsi, 1
sbb rax, -1
ret
```
Could be:
```asm
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
```
Contributor guide
Research direction
Reproduce the foo example from the linked Godbolt configuration with clang trunk, -O3, and -march=znver5, then compare the emitted x86-64 assembly with the proposed lea/test/cmovz sequence. No repository file or test is named; done means the compiler produces the requested equivalent optimization for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100