gcc的优化的说明 [英] Explanation of gcc optimization
问题描述
出于好奇黑客,我不知道如何 GCC
可管理优化低于这个功能巧妙地?
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
下面是手臂,但86是在逻辑上没有什么不同。
$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?
推荐答案
优化程序这样做分期/通行证...当你指定-O2但是也有一些让许多优化。在这里发挥作用的主要是优化
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
- loop unrolling
- constant propagation
- constant folding
- dead-code elimination
<一个href=\"http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html\">http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
所以这code
int i, j = 0;
for (i = 0; i < 10; i++) {
j += i;
}
return j;
后循环展开变为
int i, j = 0;
i=0; j += i;
i=1; j += i;
i=2; j += i;
i=3; j += i;
i=4; j += i;
i=5; j += i;
i=6; j += i;
i=7; j += i;
i=8; j += i;
i=9; j += i;
return j;
在常量传播通
int i, j = 0;
i=0; j += 0;
i=1; j += 1;
i=2; j += 2;
i=3; j += 3;
i=4; j += 4;
i=5; j += 5;
i=6; j += 6;
i=7; j += 7;
i=8; j += 8;
i=9; j += 9;
return j;
在死区code消除
j = 0;
j += 0;
j += 1;
j += 2;
j += 3;
j += 4;
j += 5;
j += 6;
j += 7;
j += 8;
j += 9;
return j;
j = 45;
return j;
最后,
return 45;
这篇关于gcc的优化的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!