gcc

gcc编译优化简介:

不开优化选项,编译器将以减少编译消耗为目的,并且使debug产生预期的结果。

各个语句都是独立的。如果在某个语句前打上断点,你可以为任意变量赋一个新值,或者改变pc寄存器的值使之指向任意语句。最终得出源码相应的期望的结果。

      

打开优化选项,编译器会以牺牲编译时间和debug能力为代价,来试图改善性能和代码量。

编译器基于对程序的了解来进行优化。一次编译多个文件到一个输出文件,编译器可以拿到所有这些文件中的信息,在编译每个文件时使用。

不是所有的优化都直接由某一个选项控制。只有列出来的部分可以。

大多优化只是在命令行开启某个-O等级的时候被enable,否则就是disabled,即使单独打开,还是以-O等级为准。

基于target和gcc的配置,每个-O等级的优化集会与列表有细微区别。可以通过gcc –Q –help=optimizers来查看具体的每个level的精确优化集。

gcc -Q –O1 --help=optimizers

gcc -Q -O2 --help=optimizers

可以看到每个-O level的优化选项的开关情况。

优化程度图示:

 

 

-O0

       default,减少编译时间,使得调试产生预期结果。

       几乎不被使用。

 

-O1

       -O即-O1。-O的default。

编译优化需要更多的时间,对大型函数来说也需要更多内存。

使用-O1, 编译器尝试减少代码大小和执行时间, 而不执行任何需要大量编译时间的优化。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.

 

 

-O2

       比-O1更加优化,gcc将执行几乎所有的不涉及到空间-速度权衡的优化点。优化时间增加,同时目标编码的性能得到提升。在O1的基础上加上更多优化开关。

Optimize even more.  GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.  As compared to -O, this option increases both compilation time and the performance of the generated code.

 

-O3

       比O2更加优化。在O2的优化点上,再加上如下优化点:

       Optimize yet more.  -O3 turns on all optimizations specified by -O2 and also turns on the

           -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize, -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone options

 

 

 

-Os

       优化size。执行

1. 所有O2级别中,不会增大code size的优化点。

2. 更多用来减小code size的优化点。

       Optimize for size.  -Os enables all -O2 optimizations that do not typically increase code size.  It also performs further optimizations designed to reduce code size. -Os disables the following optimization flags: -falign-functions  -falign-jumps  -falign-loops -falign-labels  -freorder-blocks  -freorder-blocks-and-partition -fprefetch-loop-arrays -ftree-vect-loop-version

 

 

 

-Ofast

       无视严格的标准合规性。执行:

       1. 所有O3的优化集。

       2. 一些不符合严格规定的优化点。

Disregard strict standards compliance.  -Ofast enables all -O3 optimizations.  It also enables optimizations that are not valid for all standard-compliant programs. 

It turns on -ffast-math and the Fortran-specific -fno-protect-parens and -fstack-arrays.

 

-Og

       优化debug体验。

       1. 所有不会搞乱debug的优化点。

       是在 edit-compile-debug周期应该选用的Opt level。提供一个可以保持快速编译和良好debug体验的优化等级。

Optimize debugging experience.  -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.

 

 

gcc-manual:

1.Linux: man gcc

2.download from https://gcc.gnu.org/onlinedocs/

 

posted on 2018-11-20 15:42  altc  阅读(248)  评论(0)    收藏  举报

导航