gcc都做了什么优化

直接上程序:

gcc都做了什么优化_第1张图片

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

#include <setjmp.h>
int setjmp(jmp_buf env); //返回值:若第一次直接调用则直接返回0,若从longjmp调用则返回下面的val
void longjmp(jmp_buf env, int val);

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

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

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

[henry@localhost c]$ gcc -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   more   time,   and   a   
                lot   more   memory   for   a   large   function. 
  
                With   `-O ',   the   compiler   tries   to   reduce   code   size   and   execution   
                time,   without   performing   any   optimizations   that   take   a   great   deal     ##编译器试着减少代码段的大小和代码执行时间,如果没有执行一些 
                of   compilation   time.                                                                  优化结果将花费大量编译时间。
  
                `-O '   turns   on   the   following   optimization   flags: 
                               -fdefer-pop    延迟到必要时在函数栈种pop参数
                               -fdelayed-branch    
                               -fguess-branch-probability    
                               -fcprop-registers    
                               -floop-optimize    
                               -fif-conversion    
                               -fif-conversion2    
                               -ftree-ccp    
                               -ftree-dce    
                               -ftree-dominator-opts    
                               -ftree-dse    
                               -ftree-ter    
                               -ftree-lrs    
                               -ftree-sra    
                               -ftree-copyrename    
                               -ftree-fre    
                               -ftree-ch    
                               -funit-at-a-time    
                               -fmerge-constants 
  
                `-O '   also   turns   on   `-fomit-frame-pointer '   on   machines   where   doing       ## ’-O‘也打开-fomit-frame-pointer标志当机器                    so does not interfere with debugging.                                                  这样做不会影响干涉调试。

         `-O ' doesn 't turn on `-ftree-sra ' for the Ada compiler. This
          option must be explicitly specified on the command line to be enabled for the Ada compiler.`-O2 ' 
  `-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

 

 

你可能感兴趣的:(gcc)