gcc都做了什么优化


直接上程序:

setjmp和longjmp是处理函数嵌套调用的,goto语句不能跨越函数,所以不选择goto。

#include <setjmp.h>
 setjmp(jmp_buf env); 
 longjmp(jmp_buf env,  val);

对程序进行不带优化编译:

[henry@localhost c]$  -g youhua.c -o youhua

对程序进行带优化的编译:

[henry@localhost c]$  -g -O youhua.c -o youhua_after

 

对比上面结果可以看到,全局、静态、volatile变量不受优化的影响。

  • 不进行优化时,上面定义的5个变量包括register变量都直接从内存中取值。
  • 进行优化后,register变量和局部变量gcc都是从寄存器中取的值。 

gcc都做了什么优化呢?首先可以看到变量从内存取值优化到从寄存器取值。一下是manual的部分翻译。

gcc有几个优化等级:

O0,O1,O2,O3

-O0表示没有优化,-O1为缺省值,-O3优化级别最高

'-O  
'-O1  
                Optimize.      Optimizing   compilation   takes   somewhat         memory      a   large   -O 
                -O 
                               -fdefer--fdelayed--fguess-branch--fcprop--floop--fif--fif--ftree--ftree--ftree-dominator--ftree--ftree--ftree--ftree--ftree--ftree--ftree--funit-at-a-    
                               -fmerge--O -fomit-frame-pointer标志当机器                    so does not interfere with debugging.                                                  这样做不会影响干涉调试。

         `-O t turn on `-ftree-sra
 the Ada compiler.
 `-O2 '   Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.The compiler does not perform loop unrolling or function inlining when you specify `-O2 '.As compared to `-O ',this option increases both compilation time and the performance of the generated code.   进一步的优化。GCC会支持所有不涉及时间空间交换的所有支持的优化选项。当你加入-o2选项时,编译器不会进行循环展开和函数内联。与-O选项相比,这个选项会增加编辑时间和合成码的性能。    `-O2' turns on all optimization flags specified by `-O'. It also turns on the following optimization flags:     -fthread-jumps         -fcrossjumping         -foptimize-sibling-calls         -fcse-follow-jumps     -fcse-skip-blocks         -fgcse     -fgcse-lm            -fexpensive-optimizations         -fstrength-reduce         -frerun-cse-after-loop     -frerun-loop-opt         -fcaller-saves         -fpeephole2         -fschedule-insns           -fschedule-insns2         -fsched-interblock     -fsched-spec         -fregmove         -fstrict-aliasing         -fdelete-null-pointer-checks         -freorder-blocks     -freorder-functions         -falign-functions     -falign-jumps         -falign-loops     -falign-labels         -ftree-vrp         -ftree-pre      Please note the warning under `-fgcse' about invoking `-O2' on programs that use computed gotos.           `-O3'      Optimize yet more.`-O3 ' turns on all optimizations specified by `-O2' and also turns on the `-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' options.     再一次的优化,-O3选项会添加所有-O2中添加的选项,并且添加`-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' 这三个选项     `-O0'      Do not optimize.This is the default.             -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:    -Os这个特殊的优化等级,能够实现-O2的全部不增加代码段大小优化,他强调程序的大小而不是程序的运行速度,他包含了所有第二等级的优化,除了对齐优化,这些对齐优化在体系结构的依赖性的程序中,跳过一些线性结构,循环,跳转和标签的空间,到一个指数为2的多项式和的地址。跳过这些界限可以提高性能,以及由此产生的代码和数据空间的大小,因此,这些特定的优化被禁用。

 

完!

 

参考:·[1] 

          apue

 

 

相关内容