Level 4 (-O4)

There is no level 4. Internally the compiler only goes up to level 3.

Level 3 (-O3)

At this level the compiler gets really aggressive about expanding code. It the adds the following to the level 2 optimizations:

  • -finline-functions. The compiler will try to inline any suitably simple function even if it isn't marked as "inline" in the source code. The trouble with this is that sometimes you want to prevent inlining because it increases the complexity of the final code and the compiler may spend time always preparing to execute code that in fact is only rarely executed.
  • -funswitch-loops. If a loop contains a branch whose condition is an invariant of the loop the compiler may split the loop into two loops. One loop will be optimized assuming the invariant is true and the other optimized assuming it's false. According to the Wikipedia article on loop unswitching this is done to increase speed gains from vectorization. It seems to may, though, that there may be some advantage to taking a possible branch prediction failure stall out of the loop.
    for (...) {
      A;
      if(invariant) {
        B;
      }
    }
    
    becomes
    
    if (invariant) {
      for (...) {A; B;}
    }
    else {
      for (...) A;
    }
    
  • -fgcse-after-reload. GCSE stands for Global Common Subexpression Elimination. In this case the compiler may perform an extra pass trying to eliminate needless register saves of a known result after that result has been loaded from memory.
  • No labels