cgcccompiler-optimization

how can I enable a specific set of gcc optimizations?


gcc has a large number of optimization methods, some of which correspond to command line options beginning with -f. Is there a way to enable some specific set of these and no others?

The obvious guess would be that you could do something like gcc -fauto-inc-dec a.c if you wanted just the auto-inc-dec optimization (chosen at random, I don't care about this specific one), but the manual says that will not work:

Most optimizations are completely disabled at -O0 or if an -O level is not set on the command line, even if individual optimization flags are specified.

(my emphasis). I was a little doubtful about this because I see instances on SO of people using -f flags with no -O (e.g. here), but the manual seems pretty clear that at least most of the time this does nothing.

If you use -O1 or higher, a long list of -f options is included (shown at the manual link above). I would like to know how, if it's possible, to use only a subset of these.

Motivation: I'd like to understand exactly which optimizations are responsible for some floating point quirks.


Solution

  • I was a little doubtful about this because I see instances on SO of people using -f flags with no -O (e.g. here), but the manual seems pretty clear that at least most of the time this does nothing.

    I see little reason for doubt. Manuals are not infallible, but they are much more reliable than random people's usage examples. And GNU manuals in particular are generally pretty good, because good documentation is an emphasis of the GNU project.

    If you use -O1 or higher, a long list of -f options is included (shown at the manual link above). I would like to know how, if it's possible, to use only a subset of these.

    Substantially all GCC options that function as on / off switches have both a positive and a negative form. The negative form can be constructed from the positive form by inserting no- immediately after the leading -f or -W. For example, -fno-auto-inc-dec.

    Furthermore, the options are processed as if from left to right on the command line, so if both positive and negative forms of the same option appear in the same command, including in the form of collective options such as -O1, then the last one prevails.

    Thus, unless you want only optimization options from the subset that are always available, you should turn on -O1 (or -O2, -O3, or -Os), subsequently turn off all the options thus enabled that you do not want, and lastly turn on any that you do want and are not already enabled.

    Motivation: I'd like to understand exactly which optimizations are responsible for some floating point quirks.

    Although it's useful to know about what effects various optimizations may have on your FP code, you should not overlook the likelihood that the structure and details of the code itself play the most significant role in any odd behavior you observe. Writing good FP code is a non-trivial exercise.