简体   繁体   English

在哪个级别应用了编译器优化?

[英]At which level is Compiler Optimization applied?

My question is at which level compiler applies optimization. 我的问题是哪个级别的编译器应用优化。 Is it at the level of different code files? 它是在不同的代码文件级别? If that is the case then isn't it more inefficient than say if it were applied across the whole code? 如果是这种情况,那么它是否比在整个代码中应用它更低效? Secondly, what happens when one of the source file was compiled with no optimization and then linked with one with -O3 level optimization? 其次,当一个源文件没有进行优化编译然后与一个-O3级优化链接时会发生什么?

I am especially interested in knowing how gcc handle such things. 我特别感兴趣知道gcc如何处理这些事情。

Individual translation units can be compiled with separate optimization flags, that's generally not a problem. 可以使用单独的优化标志编译单个翻译单元,这通常不是问题。 Compile-time optimizations usually only affect the visible code within one TU. 编译时优化通常仅影响一个TU内的可见代码。

An exception to that rule is the flag -fwhole-program , which indicates that your source code constitutes the entire program and allows for more aggressive optimization: 该规则的一个例外是标志-fwhole-program ,它表示您的源代码构成整个程序并允许更积极的优化:

gcc -o prog *.c -O3 -fwhole-program -s

That said, GCC has recently introduced another layer of optimisations at link time; 也就是说,GCC最近在链接时引入了另一层优化; to use this, compile everything with -flto (GCC 4.6). 使用它,用-flto (GCC 4.6)编译所有内容。 However, this is also independent of the optimization flags for each TU: 但是,这也与每个TU的优化标志无关:

gcc -c module1.c -flto -O2
gcc -c module2.c -flto -O3 -fno-strict-aliasing
gcc -c module3.c -flto -O0
gcc -c module4.c -O1
gcc -o prog module*.o -flto -s

Finally, you can also specify an independent -O* option at link stage, but I don't know if that makes any difference. 最后,您还可以在链接阶段指定一个独立的-O*选项,但我不知道这是否有所不同。

Also note that precompiled header files cannot be independently optmizied; 另请注意,预编译的头文件不能单独验证; a PCH is only eligible if it was compiled with the same optimization settings as the TU. 只有使用与TU相同的优化设置编译PCH才符合条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM