Reputation: 159
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.
Upvotes: 0
Views: 100
Reputation: 180998
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.
Upvotes: 2
Reputation: 1116
Per documentation you referred:
Most optimizations are completely disabled at -O0 or if an -O level is not set on the command line
"Most" does not mean "all".
For example, you can look at -fearly-inlining
which works always, even without optimization enabled. It does not make sense to include this option (since it is a default), but you can disable it -fno-early-inlining
.
If you want to allow just a set optimizations - you can do something like
gcc -S -O1 -fno-auto-inc-dec -fno-branch-count-reg my.c
Here -S will produce assembler code for your examination of optimization result, -O1
will turn on a set of optimization options and -fno-option
will disable some of them. Yes, in case of -O1
it would be a long list of -fno-option
and even bigger one in case of -O2
... Alas...
Please note that -O1
and -fno-option
in this case must be in this order. Since gcc/g++
do apply the last option if there is a conflict between options.
gcc -fno-auto-inc-dec -fno-branch-count-reg -O1
will disable these two option, and then enable them back due to -O1
Upvotes: 1