海湾合作委员会疯狂优化水平(-O3)不够疯狂? [英] Is the gcc insane optimisation level (-O3) not insane enough?
问题描述
作为回答另一个问题的一部分,我想说明对 gcc (
-O3
>)将基本上去掉任何未在main中使用的变量。代码是:
As part of answering another question, I wanted to show that the insane level of optimisation of gcc
(-O3
) would basically strip out any variables that weren't used in main. The code was:
#include <stdio.h>
int main (void) {
char bing[71];
int x = 7;
bing[0] = 11;
return 0;
}
和 gcc -O3
输出为:
and the gcc -O3
output was:
.file "qq.c"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
xorl %eax, %eax
movl %esp, %ebp
popl %ebp
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
.section .note.GNU-stack,"",@progbits
现在我可以看到它已被删除局部变量,但在那里仍然有相当多的浪费。在我看来,整个:
Now I can see it's removed the local variables but there's still quite a bit of wastage in there. It seems to me that the entire:
pushl %ebp
xorl %eax, %eax
movl %esp, %ebp
popl %ebp
ret
更简单:
section could be replaced with the simpler:
xorl %eax, %eax
ret
有谁知道为什么 gcc
不执行此优化?我知道这对于 main
本身会节省很少,但是,如果这也是用普通函数完成的话,那么在大量循环中不必要地调整堆栈指针的效果将是相当大的。
Does anyone have any idea why gcc
does not perform this optimisation? I know that would save very little for main
itself but, if this were done with normal functions as well, the effect of unnecessarily adjusting the stack pointer in a massive loop would be considerable.
用于生成程序集的命令为:
The command used to generate the assembly was:
gcc -O3 -std=c99 -S qq.c
推荐答案
您可以使用 -fomit-frame-pointer
编译器标志启用特定的优化。这样做使得在一些机器上调试变得不可能,而在其他所有机器上调试都变得困难,这就是为什么它通常被禁用的原因。
You can enable that particular optimization with the -fomit-frame-pointer
compiler flag. Doing so makes debugging impossible on some machines and substantially more difficult on everything else, which is why it's usually disabled.
虽然你的GCC文档可能会说 -fomit-frame-pointer
在各种优化级别启用,您可能会发现情况并非如此 - 您几乎肯定必须自己明确启用它。
Although your GCC documentation may say that -fomit-frame-pointer
is enabled at various optimization levels, you'll likely find that that's not the case—you'll almost certainly have to explicitly enable it yourself.
这篇关于海湾合作委员会疯狂优化水平(-O3)不够疯狂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!