Disable all optimization options in GCC
Stricto sensu, the GCC compiler middle-end is made of a sequence (actually a nested tree, dynamically changing during compilation) of optimization passes, so if GCC did no optimization, it won't be able to emit any code.
Think of it another way: the input language to GCC is quite rich (even for plain C, where you have while
, for
, ....) but the intermediate Gimple language is much more poor (in particular Gimple/SSA) so you need to apply some transformations to go from source AST to Gimple. These transformations are optimization passes, almost by definition.
See also the pictures from that answer and this one (an SVG image) and read the references mentioned here.
You should understand -O0
as disabling any additional optimizations (e.g. provided by -O1
etc...) not needed to produce some executable.
Well
gcc -O0 `gcc -Q -O0 --help=optimizers 2>&1 | perl -ane 'if ($F[1] =~/enabled/) {$F[0] =~ s/^\s*-f/-fno-/g;push @o,$F[0];}} END {print join(" ", @o)'` your args here
will turn all the options off (yuck).
More seriously, if you are covering all optimisation states, make a list of the optimisation flags (which you need to do anyway), and explicitly turn on or off each one with -fmyflag
or -fno-myflag
. This in essence answers your second question.
You may, however, consider it not worth your while playing with turning off optimisations that are on for all -O
levels.
As to why it's like that, that's somewhere between 'too broad' (i.e. you would have to ask whoever wrote it) and 'because that's what https://github.com/gcc-mirror/gcc/blob/master/gcc/toplev.c does'.
Note the documentation does not say that -O0
disables optimisation. It says (from the man page):
-O0
Reduce compilation time and make debugging produce the expected results. This is the default.
By implication there may be optimisations with do not increase compilation time and do not affect debugging, and these will be left on.
To answer my question, I have made some conclusions and assumptions:
So let me say that compiling with O0 does not mean that no optimizations will be applied. Options that reduce compilation time and make debugging better will be turned on as @abligh said above.
In other words, O0 is optimizing in the level of compilation. Produced binaries are not optimized in order to ease debugging process.
I give an example: This option is enabled at O0 level
-faggressive-loop-optimizations
In GCC documentation:
This option tells the loop optimizer to use language constraints to derive bounds for the number of iterations of a loop. This assumes that loop code does not invoke undefined behavior by for example causing signed integer overflows or out-of-bound array accesses. The bounds for the number of iterations of a loop are used to guide loop unrolling and peeling and loop exit test optimizations. This option is enabled by default.
So for GCC 4.8.x, there are almost 50 options turned on by default.