4

Recently, I used GCC to compile a program, but when I used -O1 optimization option, it went wrong; there was no problem when using -O0. So I replaced the -O1 with those options as the official documentation said, such as -fauto-inc-dec, -fcompare-elim, -fcprop-registers, etc. However, it works fine without any bug but the performance is not very good.

I want to know whether -O1 is equivalent to those little compilation options?

devnull
  • 118,548
  • 33
  • 236
  • 227
sosohu
  • 43
  • 1
  • 3
  • 4
    It sounds like your program actually has a bug, probably caused by undefined behaviour, that accidentally works with -O0. You should fix the bug. – Greg Hewgill Aug 06 '13 at 03:12
  • Thank you,sir. That is very possible. Howerver, I am interested in the difference from the -O1 and those optimization flags that -O1 turns on. Why do they have different effects? – sosohu Aug 06 '13 at 03:45

2 Answers2

4

Turning on optimization at all (-O1 vs -O0) changes the code generation in ways that -f flags do not control. Note this sentence in the gcc documentation:

Not all optimizations are controlled directly by a flag. Only optimizations that have a flag are listed in this section.

Some details depend on very specific gcc version numbers (e.g., gcc 4.2 vs gcc 4.5, 4.9, etc).

torek
  • 448,244
  • 59
  • 642
  • 775
1

The -O1 will turn on the following optimization flags:

      -fauto-inc-dec 
      -fcompare-elim 
      -fcprop-registers 
      -fdce 
      -fdefer-pop 
      -fdelayed-branch 
      -fdse 
      -fguess-branch-probability 
      -fif-conversion2 
      -fif-conversion 
      -fipa-pure-const 
      -fipa-profile 
      -fipa-reference 
      -fmerge-constants
      -fsplit-wide-types 
      -ftree-bit-ccp 
      -ftree-builtin-call-dce 
      -ftree-ccp 
      -ftree-ch 
      -ftree-copyrename 
      -ftree-dce 
      -ftree-dominator-opts 
      -ftree-dse 
      -ftree-forwprop 
      -ftree-fre 
      -ftree-phiprop 
      -ftree-slsr 
      -ftree-sra 
      -ftree-pta 
      -ftree-ter 
      -funit-at-a-time
jh314
  • 27,144
  • 16
  • 62
  • 82
  • I have used them to replace -O1 but the effect was not the same.Is the adding sequence also very important? – sosohu Aug 06 '13 at 03:22
  • Yes, pass sequence does matter in general. – shrm Aug 06 '13 at 03:45
  • Thank you, I want to know where can I get the sequence? GCC manual sorted it alphabetically. – sosohu Aug 06 '13 at 03:51
  • @user2655195 I am not aware of a flag which prints the pass order but I guess one can dig it out from the pass manager code. Take a look at http://gcc.gnu.org/onlinedocs/gccint/Pass-manager.html – shrm Aug 06 '13 at 04:12