x86 -O{s,2} --- several small optimization opportunities
Consider the following sample.
#include <stdlib.h>
#include <stdio.h>
int testZero(int num) {
(num - 6) & 0x7
? printf("haha")
: printf("boo");
return 0;
}
int testNum(int num) {
(num - 6) & 0x7
? printf("haha")
: printf("boo");
return num;
}
int testNumSquared(int num) {
(num - 6) & 0x7
? printf("haha")
: printf("boo");
return num * num;
}
Compile this sample for x86 with -O2 using Godbolt for trunk. The result is as follows.
testZero:
push rax
and edi,0x7
cmp edi,0x6
lea rax,[rip+0x0] # e <testZero+0xe>
R_X86_64_PC32 .L.str.1-0x4
lea rdi,[rip+0x0] # 15 <testZero+0x15>
R_X86_64_PC32 .L.str-0x4
cmove rdi,rax
xor eax,eax
call 20 <testZero+0x20>
R_X86_64_PLT32 printf-0x4
xor eax,eax
pop rcx
ret
data16 data16 cs nop WORD PTR [rax+rax*1+0x0]
testNum:
push rbx
mov ebx,edi
mov eax,edi
and eax,0x7
cmp eax,0x6
lea rax,[rip+0x0] # 42 <testNum+0x12>
R_X86_64_PC32 .L.str.1-0x4
lea rdi,[rip+0x0] # 49 <testNum+0x19>
R_X86_64_PC32 .L.str-0x4
cmove rdi,rax
xor eax,eax
call 54 <testNum+0x24>
R_X86_64_PLT32 printf-0x4
mov eax,ebx
pop rbx
ret
nop DWORD PTR [rax+rax*1+0x0]
testNumSquared:
push rbx
mov ebx,edi
mov eax,edi
and eax,0x7
cmp eax,0x6
lea rax,[rip+0x0] # 72 <testNumSquared+0x12>
R_X86_64_PC32 .L.str.1-0x4
lea rdi,[rip+0x0] # 79 <testNumSquared+0x19>
R_X86_64_PC32 .L.str-0x4
cmove rdi,rax
xor eax,eax
call 84 <testNumSquared+0x24>
R_X86_64_PLT32 printf-0x4
imul ebx,ebx
mov eax,ebx
pop rbx
ret
For testZero, observe the following optimization opportunities.
- Instead of push rax, and later xor eax, eax then pop rcx, the code could have done push 0 then pop rax. Alternatively, it could have delayed push rax until after xor eax, eax right before printf, saving the need for another xor eax, eax later (and then pop rcx becomes pop rax). This is also missed for -Os.
- The second lea might not need to be rip relative since it could be resolved as a simple lea against the immediately preceding rip relative load once the addresses of the literals are known. The advantage is that the second rip relative load could avoid a 4 byte immediate if the literals are close enough, so the instruction is smaller. This is also missed for -Os.
For testNum, observe the following optimization opportunities in addition to #2 for testZero.
- Instead of push rbx then pop rbx, the code could have done push rdi then pop rax, avoiding some mov instructions to save edi in ebx and then restoring ebx to eax in the process.
- It is unnecessary to move edi to eax before doing arithmetic (note how testZero does not do this).
For testNumSquared, observe the following optimization opportunities in addition to #2 for testZero, and #2 for testNum.
- Could consider doing imul ebx, ebx immediately after mov ebx, edi to hide that latency behind the calls to printf.
Generally speaking, the rationale for these is that fewer instructions are better, and that smaller code is better, per the manual.
- testZero-1: this should, if at all, only be done at Os/Oz. push rax is the shortest encoding to align the stack;
push 0; pop raxcreates an unnecessary dependency. (The saving here is 2 bytes.) - testZero-2: impossible, because we don't know where the strings end up when string sections are merged+deduplicated at link time -- and even if we were able to express the difference as reloc (which we aren't on x86), we'd have to assume a 4 byte difference, causing no code size change but an extra dependency, making things slower.
- testNum-2: hm, num gets assigned to ebx and regalloc (twoaddress, really) creates another copy of ebx, which is later replaced with a copy of edi. Hard to fix and little benefit as moves are usually free the code is unless front-end bound.
- testNumSquared-1: Unlikely to matter, return addresses are very often predicted correctly.
From a performance perspective, I'd consider it unlikely that these changes make a measurable difference on real code.
@llvm/issue-subscribers-backend-x86
Author: sqrmax
#include <stdlib.h>
#include <stdio.h>
int testZero(int num) {
(num - 6) & 0x7
? printf("haha")
: printf("boo");
return 0;
}
int testNum(int num) {
(num - 6) & 0x7
? printf("haha")
: printf("boo");
return num;
}
int testNumSquared(int num) {
(num - 6) & 0x7
? printf("haha")
: printf("boo");
return num * num;
}
Compile this sample for x86 with -O2 using Godbolt for trunk. The result is as follows.
testZero:
push rax
and edi,0x7
cmp edi,0x6
lea rax,[rip+0x0] # e <testZero+0xe>
R_X86_64_PC32 .L.str.1-0x4
lea rdi,[rip+0x0] # 15 <testZero+0x15>
R_X86_64_PC32 .L.str-0x4
cmove rdi,rax
xor eax,eax
call 20 <testZero+0x20>
R_X86_64_PLT32 printf-0x4
xor eax,eax
pop rcx
ret
data16 data16 cs nop WORD PTR [rax+rax*1+0x0]
testNum:
push rbx
mov ebx,edi
mov eax,edi
and eax,0x7
cmp eax,0x6
lea rax,[rip+0x0] # 42 <testNum+0x12>
R_X86_64_PC32 .L.str.1-0x4
lea rdi,[rip+0x0] # 49 <testNum+0x19>
R_X86_64_PC32 .L.str-0x4
cmove rdi,rax
xor eax,eax
call 54 <testNum+0x24>
R_X86_64_PLT32 printf-0x4
mov eax,ebx
pop rbx
ret
nop DWORD PTR [rax+rax*1+0x0]
testNumSquared:
push rbx
mov ebx,edi
mov eax,edi
and eax,0x7
cmp eax,0x6
lea rax,[rip+0x0] # 72 <testNumSquared+0x12>
R_X86_64_PC32 .L.str.1-0x4
lea rdi,[rip+0x0] # 79 <testNumSquared+0x19>
R_X86_64_PC32 .L.str-0x4
cmove rdi,rax
xor eax,eax
call 84 <testNumSquared+0x24>
R_X86_64_PLT32 printf-0x4
imul ebx,ebx
mov eax,ebx
pop rbx
ret
For testZero, observe the following optimization opportunities.
- Instead of push rax, and later xor eax, eax then pop rcx, the code could have done push 0 then pop rax. Alternatively, it could have delayed push rax until after xor eax, eax right before printf, saving the need for another xor eax, eax later (and then pop rcx becomes pop rax). This is also missed for -Os.
- The second lea might not need to be rip relative since it could be resolved as a simple lea against the immediately preceding rip relative load once the addresses of the literals are known. The advantage is that the second rip relative load could avoid a 4 byte immediate if the literals are close enough, so the instruction is smaller. This is also missed for -Os.
For testNum, observe the following optimization opportunities in addition to #2 for testZero.
- Instead of push rbx then pop rbx, the code could have done push rdi then pop rax, avoiding some mov instructions to save edi in ebx and then restoring ebx to eax in the process.
- It is unnecessary to move edi to eax before doing arithmetic (note how testZero does not do this).
For testNumSquared, observe the following optimization opportunities in addition to #2 for testZero, and #2 for testNum.
- Could consider doing imul ebx, ebx immediately after mov ebx, edi to hide that latency behind the calls to printf.
Generally speaking, the rationale for these is that fewer instructions are better, and that smaller code is better, per the manual.
@aengelke Hi, so is it worth spending time on this issue from a performance perspective? or should I focus on fixing some other issue?
I guess the testZero-2 subissue can become a feature request for LTO (link time optimization).