[ACCEPTED]-What's the difference between -O3 and (-O2 + flags that man gcc says -O3 adds to -O2)?-gcc

Accepted answer
Score: 23

Man pages can be outdated, but you can find 22 actual lists for O2 and O3.

To get full list 21 (almost, check "update") of -f optimization 20 options actually used, I suggest you use 19 the -fverbose-asm -save-temps (or -fverbose-asm -S) - there is a full list at a 18 top of asm file (*.s).

For gcc-4.6.0 I got 17 x (the difference between O2 and O3) to be:

 -fgcse-after-reload
 -finline-functions
 -fipa-cp-clone
 -fpredictive-commoning
 -ftree-loop-distribute-patterns
 -ftree-vectorize
 -funswitch-loops

Another 16 source of information for your question 15 is the sources of GCC (file gcc/opts.c and possibly 14 gcc/common.opt) as gcc-4.6.0:

/* -O3 optimizations.  */
{ OPT_LEVELS_3_PLUS, OPT_ftree_loop_distribute_patterns, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_fpredictive_commoning, NULL, 1 },
/* Inlining of functions reducing size is a good idea with -Os
   regardless of them being declared inline.  */
{ OPT_LEVELS_3_PLUS_AND_SIZE, OPT_finline_functions, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_funswitch_loops, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_fgcse_after_reload, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_ftree_vectorize, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_fipa_cp_clone, NULL, 1 },

I have also checked, does 13 gcc check -On in other files (cscope symbol 12 search for x_optimize).

The only additional usage of 11 n from option -On is saving it value into macro 10 __OPTIMIZE__. So some headers can behave differently 9 for value of this macro equal 2 or 3.

UPDATE: There are questions about it in GCC WIKI:

  • "Is -O1 (-O2,-O3 or -Os) equivalent to individual -foptimization options?"

No. First, individual 8 optimization options (-f*) do not enable 7 optimization, an option -Os or -Ox with 6 x > 0 is required. Second, the -Ox flags 5 enable many optimizations that are not controlled 4 by any individual -f* option. There are no plans to add individual options for controlling all these optimizations.

  • "What specific flags are enabled by -O1 (-O2, -O3 or -Os)?"

Varies by 3 platform and GCC version. You can get GCC 2 to tell you what flags it enables by doing 1 this:

touch empty.c
gcc -O1 -S -fverbose-asm empty.c
cat empty.s
Score: 3

If your compiler hangs, then yes - I would 11 regard that as a compiler bug. Compilers 10 have bugs too.

(even if the compiler used 9 to compile your compiler have bugs, a bug 8 could be introduced in the new compiler 7 - gcc takes some steps to avoid that by 6 its staging bootstrapping though.)

It could 5 be other things as well, e.g. the optimization 4 done just takes much, much more time to 3 perform, or the increased optimization level 2 causes more memory to be used, and your 1 system starts trashing.

More Related questions