Suppose that we have a function f that can be written in 2 ways with
identical result:
unsigned int f(unsigned int i, unsigned int n) {++i; if (i == n) ++i;
return i;}
unsigned int f(unsigned int i, unsigned int n) {++i; i += i == n; return i;}
g++ -O3 produces different code for the 2 versions:
pushl %ebp
.LCFI0:
+ xorl %edx, %edx
movl %esp, %ebp
.LCFI1:
- movl 8(%ebp), %edx
- leal 1(%edx), %eax
+ movl 8(%ebp), %eax
+ incl %eax
cmpl 12(%ebp), %eax
- je .L6
popl %ebp
- ret
- .p2align 4,,7
-.L6:
- popl %ebp
- leal 2(%edx), %eax
+ sete %dl
+ addl %edx, %eax
ret
.LFE2:
This implies that one of the following 3 statements holds:
1. The 2 versions of f are indeed not identical.
2. The 2 versions of the generated code are equally efficient, so the
difference does not matter.
3. g++ generates suboptimal code for one of the versions of f.
Anyone knows which statement holds.