10

How to turn off all optimizations in GCC? Using -O0 does not work since it still optimizes out the statements that have no effects, or any code that is after an infinite loop without any break statements.

user2124324
  • 161
  • 1
  • 8
  • There is none, all I have is -g -gstrict-dwarf -Wall – user2124324 Nov 27 '13 at 16:57
  • I don't think you can turn off all optimizations. –  Nov 27 '13 at 16:57
  • Even without any flags gcc does some basic optimizations. It makes sense to remove statements with no effects (optimization or not). Just curious: why do you want to keep dead code? – P.P Nov 27 '13 at 16:59
  • This is for test framework purposes – user2124324 Nov 27 '13 at 17:00
  • Here: http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Optimization-Levels.html it is states that With gcc, it is very unusual to use -O0 for production if execution time is of any concern, since -O0 really does mean no optimization at all. This difference between gcc and other compilers should be kept in mind when doing performance comparisons. The question is whether you really care about compilation and debuging optimizations.. I belive you can't read of it as well as you can't read from compilation itself. – klm123 Nov 27 '13 at 17:09

2 Answers2

9

There is no way to make gcc not ignore unreachable code and statments that have no effect.

What you can do is make code that is unreachable appear to be reachable by using volatile variables.

volatile bool always_true = true;

if( always_true  )
{
     //infinite loop
     //return something
}

//Useless code

in the above example, gcc won't optomize out useless code because it cannot know it is infact useless

int a = 5;
int b = 5;
volatile int c = 9;

c += 37;
return a + b;

In this example, integer c won't be optimized out because gcc does can't know it is dead weight code.

8bitwide
  • 2,071
  • 1
  • 17
  • 24
  • With `gcc -O0`, volatile isn't necessary, as long as you don't use `const`. All non-`const` variables are assumed to have possibly changed value between C statements, that's [why compilers don't keep values in registers across statements in -O0 "debug builds"](https://stackoverflow.com/questions/53366394/why-does-clang-produce-inefficient-asm-with-o0-for-this-simple-floating-point) (a lot like `volatile`). So `if (variable)` is never dead code, but `if (0)` or `if (const)` are, at `-O0` – Peter Cordes May 08 '22 at 05:05
  • But yes, if you want to be able to enable optimization instead of gimping your whole program just for this branch, yes use `volatile`! – Peter Cordes May 08 '22 at 05:49
3

You have to make your code nearly impossible to be optimized by the compiler. For example:

  • use volatile keyword on variables that you wish not to be optimized
  • make sure the code has effect, for example: not just only changing the variable value but also print the value or store it to another variable or do arithmetic to the variable and store it in another variable
  • reference/change the variable in other function to make sure the compiler cannot judge it is not used in compile time
dragon135
  • 1,366
  • 8
  • 19