GCC優化選項的各種含義以及潛藏風險
在研究編譯驅動的makefile的時候,發現GCC的命令列裡面有一個-Os的優化選項。
遍查GCC文件,發現了-O0, -O1, -O2, -O3,就是沒有發現-Os。
祭出GOOGLE大法搜了一下,終於發現這篇文章說明了-Os的作用:
http://www.linuxjournal.com/article/7269
原來-Os相當於-O2.5。是使用了所有-O2的優化選項,但又不縮減程式碼尺寸的方法。
詳細的說明如下:
Level 2.5 (-Os)
The special optimization level (-Os or size) enables all -O2 optimizations that do not increase code size; it puts the emphasis on size over speed. This includes all second-level optimizations, except for the alignment optimizations. The alignment optimizations skip space to align functions, loops, jumps and labels to an address that is a multiple of a power of two, in an architecture-dependent manner. Skipping to these boundaries can increase performance as well as the size of the resulting code and data spaces; therefore, these particular optimizations are disabled. The size optimization level is enabled as:
gcc -Os -o test test.c
In gcc 3.2.2, reorder-blocks is enabled at -Os, but in gcc 3.3.2 reorder-blocks is disabled.
==============================
補充:在GCC的官方文件裡又發現了關於-Os的說明:
http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.html#Optimize-Options