开发者

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开发者_StackOverflow optimization?

I am especially interested in knowing how gcc handle such things.


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.

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:

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

That said, GCC has recently introduced another layer of optimisations at link time; to use this, compile everything with -flto (GCC 4.6). However, this is also independent of the optimization flags for each 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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜