Out of hacker curiosity, I wonder how gcc can manage to optimize the function below this smartly?
int c() {
int i, j = 0;
for (i = 0; i < 10; i++) {
j += i;
}
return j;
}
$objdump -D c.o below is for arm but x86 is no different in logic.
00000000 <c>:
0: 202d movs r0, #45 ; 0x2d
2: 4770 bx lr
I mostly wonder if this is result of a chain of optimizations or something like a template match? Are there any documentation on such optimizations?
The optimizer does this in phases/passes… when you specify -O2 there are many optimizations that are enabled. The principal optimizations that come into play here are
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
so this code
after loop unrolling becomes
after constant propagation pass
after dead-code elimination
after constant folding
and finally,