Search code examples
optimizationgcccompilationcompiler-flags

How to enable single optimisation flags in gcc?


When using gcc, can individual optimisation flags be enabled without a -O level being specified?

gcc -ffasst-math foo.c

OR

gcc -O1 -ffast-math foo.c

Which one works? Thanks!


Solution

  • Yes, you can enable individual optimization flags.

    Info from the gcc man page:

    -O

    -O turns on the following optimization flags:

    -fauto-inc-dec -fcprop-registers -fdce -fdefer-pop -fdelayed-branch -fdse -fguess-branch-probability -fif-conversion2 -fif-conversion -finline-small-functions -fipa-pure-const -fipa-reference -fmerge-constants -fsplit-wide-types -ftree-builtin-call-dce -ftree-ccp -ftree-ch -ftree-copyrename -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-fre -ftree-sra -ftree-ter -funit-at-a-time

    -O also turns on -fomit-frame-pointer on machines where doing so does not interfere with debugging.

    -ffast-math

    Sets -fno-math-errno, -funsafe-math-optimizations, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and -fcx-limited-range. This option causes the preprocessor macro "FAST_MATH" to be defined.

    This option is not turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications for math functions. It may, however, yield faster code for programs that do not require the guarantees of these specifications.