您的位置:首页 > 编程语言 > C语言/C++

GCC优化选项简单说明

2015-11-27 12:21 435 查看
最近一直质疑gcc的-O优化选项究竟做了什么,因此,在网上找了好多相关的资料,但都没有明确的进行说明,以下是网上一些资料的汇总和偶自己很水很水的翻译,模模糊糊的理解了,-O的一些优化功能,希望大牛们给于进一步的解释,和纠错~~~~谢谢~!

gcc默认提供了5级优化选项的集合:

-O0:无优化(默认)

-O和-O1:使用能减少目标文件大小以及执行时间并且不会使编译时间明显增加的优化.在编译大型程序的时候会显著增加编译时内存的使用.

-O2: 包含-O1的优化并增加了不需要在目标文件大小和执行速度上进行折衷的优化.编译器不执行循环展开以及函数内联.此选项将增加编译时间和目标文件的执行性能.

-Os:专门优化目标文件大小,执行所有的不增加目标文件大小的-O2优化选项.并且执行专门减小目标文件大小的优化选项.

-O3: 打开所有-O2的优化选项并且增加 -finline-functions, -funswitch-loops,-fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize优化选项.

-O1包含的选项-O1通常可以安全的和调试的选项一起使用:

`-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选项,编译器会尽可能的缩短代码长度和运行时间,不会执行一切耗费大量时间的优化行为。

   `-O ' turns on the following optimization flags:

  -O选项会添加以下的优化标志 

  -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 so does not interfere with debugging. 

  当在机子上,运行无法干涉到调试时,-O选项也会添加-fomit-frame-pointer选项

  `-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. 

  在ADA编译器上,-O不会添加-ftree-sra选项,想在ADA编译器下使用该选项,必须明确的在命令行指出。

  

`-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:

  -O2选项会添加-O选项中添加的所有选项,同时,他也会添加下列选项标志。 

    -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. 

  在引用了优化处理的计算跳转的程序理,请在-fgcse选项中注明警告。

  

    `-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的多项式和的地址。跳过这些界限可以提高性能,以及由此产生的代码和数据空间的大小,因此,这些特定的优化被禁用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++