Hello,

I'm trying to use the compiler optimizations for GCC 4.3.3. I used the following LINKas a reference. I'm mainly using the O2 and Os levels of optimizations. Could anyone please let me know if the order in which the options matters?

Thanks in advance!

Dani AI

Generated

Short answer: yes — order matters, but only in well-defined ways. The last -O level on the command line is the one that takes effect (so -O2 -Os behaves like -Os). Individual -f flags (and other more specific switches) are used to fine‑tune or undo what an -O level enabled; more specific options take priority over less specific ones, and for options of the same specificity the last one wins. (gcc.gnu.org)

Clarification and examples tied to the thread: was correct to advise using a single -O level as a baseline. In practice a typical workflow is to pick the appropriate level (-O2 for general speed, -Os for size) and then explicitly enable/disable particular passes with -f/-fno- flags when necessary. For example -O2 turns on -finline-small-functions while -finline-functions is enabled at -O3; adding -fno-inline will stop inlining regardless of the -O level because the explicit -f form is more specific. (gcc.gnu.org)

How to inspect and verify what a given command line actually enables (useful for benchmarking or debugging): the driver supports -Q --help=optimizers which reports which optimizer flags are enabled for the current command line. Typical commands:

gcc -c -Q -O2 --help=optimizers
gcc -c -Q -Os --help=optimizers
gcc -c -Q -O2 -fno-inline --help=optimizers
gcc -S -O2 source.c    # inspect produced assembly

This output can be diffed between levels to see exactly what changed. (chiark.greenend.org.uk)

Practical tips that follow from the above (and from ’s benchmark suggestion): pick one clear optimization level per build target, override only specific flags when a measured problem appears, check the assembly (-S) when behavior or performance is surprising, and use -Og/-g for debug-friendly builds. Note that when linking without an explicit -O, the linker/driver may pick an optimization level based on the object files; the highest level among them is used in that case, so be explicit at link time for reproducibility. (gcc.gnu.org)

References: GCC 4.3.3 Optimize Options. (gcc.gnu.org)

Recommended Answers

All 2 Replies

I’m not positive, because I am far from an expert on GCC, but I believe you can (or at least should) only select one optimization level at a time. There are a number of different things that could be done to optimize a program. In GCC, these are controlled by individual flags, each of which controls a specific optimization action. I believe that the different levels end up setting different combinations of optimization flags. In the version I use, I can set a level, and I can also control some other optimization flags from the IDE. If you select more than one level at a time, you may end up with a combination of flags.

You could try setting various combinations of levels, and then look at what flags actually get set.

Keep in mind that optimizing for one attribute may be detrimental to another. In general, with any compiler, going from no optimization to the first level of optimization will result in an increase in speed and a decrease in size. This is because a bunch of unnecessary stuff gets thrown out. When you start optimizing beyond that, you may start trading off speed for size. Optimization can also play havoc with debugging, particularly single stepping. And some optimizations may mess with others. (That is, “Some combinations of flags may result in code that does not work as expected!”)

I would recommend that you come up with some sort of benchmark so you have an idea of the effect of different optimizations. Size is easy, you can just look at the resulting code size. Speed is a bit more challenging. You may not even need any speed optimization, but only you can make that determination.

You may want to get a copy of the book “An Introduction to GCC for the GNU Compiler gcc and g++”, written by Brian Gough. Published by Network Theory Limited ISBN 0-9541617-9-3

Thanks for the detailed explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.