https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
GCC提供选项控制代码的优化等级,这些选项可以实现不同程度的优化。
没有任何优化选项的话,编译时的目标是降低编译成本,并使调试产生预期的结果。语句是独立的:如果你使程序停止在语句之间设置的断点处,你可以改变任何变量的值或者改变程序计数器的值使程序执行任意函数中的语句并得到源代码中期望的结果。
打开优化标志使编译器尝试以编译时间和可能的调试程序的能力为代价来改进性能和/或代码大小。
编译器根据程序的信息执行优化。 一次编译多个文件生成单个可执行文件的模式允许编译器在编译每个文件时使用从所有文件中获取的信息。
并非所有优化都由标志直接控制。以下仅列出具有标志的优化。
只有在命令行上设置-O或其他等级的优化选项时,才会启用大多数优化。 否则,即使指定了单独的优化标志,它们也会被禁用。
根据目标和GCC的配置方式,可以在每个-O级别启用略有不同的优化集。
-O、-O1:优化编译需要更多时间,并且大型函数需要更多内存。使用-O选项,编译器会尝试减小代码尺寸减少执行时间,不执行任何需要大量编译时间的优化。
-O选项打开了如下优化标志:
-fauto-inc-dec
-fbranch-count-reg
-fcombine-stack-adjustments
-fcompare-elim
-fcprop-registers
-fdce
-fdefer-pop
-fdelayed-branch
-fdse
-fforward-propagate
-fguess-branch-probability
-fif-conversion
-fif-conversion2
-finline-functions-called-once
-fipa-modref
-fipa-profile
-fipa-pure-const
-fipa-reference
-fipa-reference-addressable
-fmerge-constants
-fmove-loop-invariants
-fomit-frame-pointer
-freorder-blocks
-fshrink-wrap
-fshrink-wrap-separate
-fsplit-wide-types
-fssa-backprop
-fssa-phiopt
-ftree-bit-ccp
-ftree-ccp
-ftree-ch
-ftree-coalesce-vars
-ftree-copy-prop
-ftree-dce
-ftree-dominator-opts
-ftree-dse
-ftree-forwprop
-ftree-fre
-ftree-phiprop
-ftree-pta
-ftree-scev-cprop
-ftree-sink
-ftree-slsr
-ftree-sra
-ftree-ter
-funit-at-a-time
-O2:相对-O优化更多
GCC几乎执行所有支持的优化,但不涉及空速权衡。 与-O相比,此选项增加了编译时间和生成代码的性能。
-O2除了打开所有-O指定的优化标志,还打开了如下优化标志:
-falign-functions -falign-jumps
-falign-labels -falign-loops
-fcaller-saves
-fcode-hoisting
-fcrossjumping
-fcse-follow-jumps -fcse-skip-blocks
-fdelete-null-pointer-checks
-fdevirtualize -fdevirtualize-speculatively
-fexpensive-optimizations
-ffinite-loops
-fgcse -fgcse-lm
-fhoist-adjacent-loads
-finline-functions
-finline-small-functions
-findirect-inlining
-fipa-bit-cp -fipa-cp -fipa-icf
-fipa-ra -fipa-sra -fipa-vrp
-fisolate-erroneous-paths-dereference
-flra-remat
-foptimize-sibling-calls
-foptimize-strlen
-fpartial-inlining
-fpeephole2
-freorder-blocks-algorithm=stc
-freorder-blocks-and-partition -freorder-functions
-frerun-cse-after-loop
-fschedule-insns -fschedule-insns2
-fsched-interblock -fsched-spec
-fstore-merging
-fstrict-aliasing
-fthread-jumps
-ftree-builtin-call-dce
-ftree-pre
-ftree-switch-conversion -ftree-tail-merge
-ftree-vrp
-O3:相对-O2优化更多。
-O3除了打开所有-O2指定的优化标志,还打开了如下优化标志:
-fgcse-after-reload
-fipa-cp-clone
-floop-interchange
-floop-unroll-and-jam
-fpeel-loops
-fpredictive-commoning
-fsplit-loops
-fsplit-paths
-ftree-loop-distribution
-ftree-loop-vectorize
-ftree-partial-pre
-ftree-slp-vectorize
-funswitch-loops
-fvect-cost-model
-fvect-cost-model=dynamic
-fversion-loops-for-strides
-O0:减少编译时间并使调试可以产生预期的结果,这也是默认的优化等级。
-Os:优化尺寸。 -Os启用所有通常不会增加代码大小的-O2优化。 它还执行旨在减少代码大小的进一步优化。
-Os关闭如下优化标志:
-falign-functions -falign-jumps
-falign-labels -falign-loops
-fprefetch-loop-arrays -freorder-blocks-algorithm=stc
-Ofast:无视严格的标准合规性。 -Ofast启用所有-O3优化。 它还打开并非对所有符合标准的程序有效的优化。 它打开
-ffast-math和Fortran特定的-fno-protect-parens和-fstack-arrays。
-Og:优化调试体验。 -Og启用不会干扰调试的优化。 它是标准编辑 - 编译 - 调试周期可以选择的优化级别,提供合理的优化级别,同时保持快速编译和良好的调试体验。
-fbranch-count-reg -fdelayed-branch
-fdse -fif-conversion -fif-conversion2
-finline-functions-called-once
-fmove-loop-invariants -fssa-phiopt
-ftree-bit-ccp -ftree-dse -ftree-pta -ftree-sra
如果使用多个-O选项(包含或不包含级别编号),则最后一个选项是有效的选项。