1

While at University I learned that compiler optimises our code, in order for the executable to be faster. For example when a variable is not used after a point, it will not be calculated.

So, as far as I understand, that means that if I have a program that calls a sorting algorithm, if the results of the algorithm are printed then the algorithm will run. However, if nothing is printed(or used anywhere else), then there is no reason for the program to even make that call.

So, my question is:

Does these things(optimisation) happen by default when compiling with gcc? Or only when the code is compiled with O1, O2, O3 flags?

Jim Blum
  • 2,656
  • 5
  • 28
  • 37
  • 1
    [Optimize Options](http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) might help. – devnull Jul 03 '14 at 08:52
  • Thanks a lot @devnull. There are dozens of options that I do not even understand. Do you know what specific option is used for this kind of optimisation? – Jim Blum Jul 03 '14 at 09:04

1 Answers1

4

When you meet a new program for the first time, it is helpful to type man followed by the program name. When I did it for gcc, it showed me this:

Most optimizations are only enabled if an -O level is set on the command line. Otherwise they are disabled, even if individual optimization flags are specified.

...

-O0 Reduce compilation time and make debugging produce the expected results. This is the default.

To summarize, with -O0, all code that is in the execution path that is taken will actually execute. (Program text that can never be in any execution path, such as if (false) { /* ... */ }, may not generate any machine code, but that is unobservable.) The executed code will feel "as expected", i.e. it'll do what you wrote. That's the goal, at least.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Yes, I have seen that before. But it does not say explicitly if optimisation is enabled or not. Is it? – Jim Blum Jul 03 '14 at 08:49
  • ohhh, I understand. So the program is compiled without any optimisation. Therefore anything called will be called even if it is not used afterwards. thanks – Jim Blum Jul 03 '14 at 08:50
  • 1
    @JimBlum: Well, it says that most optimizations are enabled *only if* you have a non-zero O-level, so it's probably safe to assume that with `-O0` you don't get any significant optimizations. I'm not sure if you count codegen details like whether to use shift or multiply an optimization; I suppose `x / 2` may be implemented with a shift even at `-O0`. – Kerrek SB Jul 03 '14 at 08:51
  • @JimBlum: That said, trivial dead code removal may happen even at `-O0`, such as `if (false) { /* never emitted */ }`, or `bool b; switch (b) { case true: case false: default: }`. But yes, any deep kind of reachability analysis is definitely not done at `-O0`. – Kerrek SB Jul 03 '14 at 08:54
  • thanks a lot for your answer and your help. In fact it is still not very clear for me whether the sorting algorithm example I mentioned before will be called or skipped at default level if not used afterwards? – Jim Blum Jul 03 '14 at 08:59
  • @JimBlum: You can always compile and look at the machine code! But I'd say that "produce the expected results" means that your code will run, since that's what you expect if you write it in a path that is taken. – Kerrek SB Jul 03 '14 at 09:08
  • If `-O0` did not apply any optimization, then I would not have to ask this question: http://stackoverflow.com/questions/24541589/adding-two-floating-point-numbers – Pascal Cuoq Jul 03 '14 at 10:54
  • "`if (false)...` is unobservable": not quite. You can disassemble the generated code. And a debugger might even let you set PC to the instructions there. – laune Jul 03 '14 at 11:10
  • @laune: "Unobservable" from the point of view of the expected behaviour of the code. Of course any result of the translation is observable to the outside view... – Kerrek SB Jul 03 '14 at 11:34