views:

471

answers:

5

I know that -O1 automatically turns on certain flags. These flags can be turned on manually though. If I don't specify -O1, it should still be possible to get -O1 optimization by specifying all the flags that -O1 turns on.

I tried

-fthread-jumps -fcprop-registers -fguess-branch-probability

but it still does not do -O1 optimization. I can tell when I use gprof because the performance is not as good.

Which flags do I turn on to get -O1 optimization?

+2  A: 

This depends on your version of gcc. See the gcc manpage.

On my machine, -O (which is -O1) turns on the following optimizations:

   -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.
Stephan202
+6  A: 

From the manual:

-O
-O1
Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function. 

With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. 

-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 
          -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-forwprop 
          -ftree-fre 
          -ftree-phiprop 
          -ftree-sra 
          -ftree-pta 
          -ftree-ter 
          -funit-at-a-time
TheGrandWazoo
There are other internal flags that cannot be "turned on" to simulate a full `-O1`.
LiraNuna
+10  A: 

One way to find out:

gcc -O1 -c -Q -v dummy.c

(where dummy.c is your filename.) This causes gcc to spew the flags used to the command line.

Edit: Please see kastauyra's answer on this. It appears you cannot simulate full -O1 optimization with -f flags alone.

int3
+4  A: 

Unfortunately, that's impossible. There are a lot of individual optimization flags turned on by -O1, true, however a lot of code in GCC checks global optimization flag value and performs optimizations not specified by any of -f.. options.

Laurynas Biveinis
hmm that's news to me.. can quote doc?
int3
The doc does not say that (probably a patch to change this would be accepted). But this can be seen from the source code (check for number of places if (optimize > 0) ... which do not check individual flags), also this has been asked on GCC mailing lists: http://gcc.gnu.org/ml/gcc-help/2008-02/msg00389.html
Laurynas Biveinis
A: 

You can also try to use this pragma (it requires GCC >= 4.4) :

#pragma GCC optimize opt_list
void f()

This pragma allow you to turn on and off specific optimizations for given function. opt_list is list of -f* options without -f.

You also can change global optimization level:

#pragma GCC optimize 1
#pragma GCC optimize 0

You also can use

#pragma GCC optimization_level n

and for Intel C compiler #pragma intel optimization_level n

osgx