GCC:Optimization
lastmod 2020-09-16created 2015-11-105 edits
By default, if no options are given, gcc does not optimize the code it generates. It provides many options for optimization, but rather than picking these options one by one, people generally use -O, -O1, -O2, -O3, etc., which bundle these options into sets.
(Based on gcc version 3.2.2, 3.4.6.)
(For reference, -O0 is the default option that means no optimization at all.)
Optimization set
-
-O,-O1 - Performs optimization that keeps the generated object or executable as small as possible, within a range that does not make compilation take long.
-
-O2 - Makes the generated code run as fast as possible, but performs optimization within a range that keeps the code size from growing too large.
-
-Os - Uses all the optimizations provided by -O2, but performs optimization after leaving out those that increase code size.
-
-O3 - Performs optimization solely to produce fast code, with no regard at all for code size. However, keep in mind that there is no guarantee that code built with
-O3is faster than code built with-O2. The reason is that when a CPU executes machine code, it usually first loads a certain amount into the CPU's internal cache and executes it from there, and code built with-O3is generally larger, so relatively fewer instructions fit in this cache, which may actually make it slower.
You can also mix other options with options of the form -On (n=1, 2, 3). For example, when using -O2, inline-function optimization is not applied. So, as below, you can use all the optimization techniques of -O2 together with -inline-functions.
Of course, you can also turn off a particular option. The following shows compiling with all the optimization techniques of -O3 applied, except for the inline-functions optimization.
In short, to turn an additional option on, use -f<OPTION_NAME>, and to turn it off, use the form -fno-<OPTION_NAME>.
GCC Optimization Option List
The list of optimization options is below. Whether each is used by the optimization set options is also summarized.
| Optimization | -O1 | -O2 | -Os | -O3 | Remarks |
| defer-pop | O | O | O | O | |
| thread-jumps | O | O | O | O | |
| branch-probabilities | O | O | O | O | |
| cprop-registers | O | O | O | O | |
| guess-branch-probability | O | O | O | O | |
| omit-frame-pointer | O | O | O | O | |
| merge-constants | O | O | O | O | |
| loop-optimize | O | O | O | O | |
| if-conversion | O | O | O | O | |
| if-conversion2 | O | O | O | O | |
| align-loops | X | O | X | O | |
| align-jumps | X | O | X | O | |
| align-labels | X | O | X | O | |
| align-functions | X | O | X | O | |
| crossjumping | X | O | O | O | |
| prefetch-loop-array | ? | ? | X | ? | |
| optimize-sibling-calls | X | O | O | O | |
| cse-follow-jumps | X | O | O | O | |
| cse-skip-blocks | X | O | O | O | |
| gcse | X | O | O | O | |
| gcse-lm | X | O | O | O | |
| gcse-sm | X | O | O | O | |
| gcse-las | X | O | O | O | |
| expensive-optimizations | X | O | O | O | |
| strength-reduce | X | O | O | O | |
| rerun-cse-after-loop | X | O | O | O | |
| rerun-loop-opt | X | O | O | O | |
| caller-saves | X | O | O | O | |
| force-mem | X | O | O | O | |
| peephole2 | X | O | O | O | |
| regmove | X | O | O | O | |
| strict-aliasing | X | O | O | O | For the compiler, variables of different types do not point to the same memory. (Details) |
| delete-null-pointer-checks | X | O | O | O | |
| reorder-blocks | X | O | O | O | |
| reorder-functions | X | O | O | O | |
| unit-at-a-time | X | O | ? | O | |
| schedule-insns | X | O | O | O | |
| schedule-insns2 | X | X | X | O | |
| schedule-interblock | X | O | ? | O | |
| sched-spec | X | O | ? | O | |
| inline-functions | X | X | X | O | |
| rename-registers | X | X | X | O | |
| web | X | X | X | O | |
| unswitch-loops | X | X | ? | O |
FORTIFY SOURCE
When FORTIFY_SOURCE is applied, common memory/string and buffer-related functions are replaced with GCC builtin functions, and these can only perform overflow checks if they know the size of the target of the operation (Destination, i.e., the area where the result is stored). The __builtin_object_size() built-in function is used to find out the size of such memory areas.
Usage is as follows.
- An optimization option of
-O1or higher. - Add a define with
-D_FORTIFY_SOURCE=N.
See also
Favorite site
References
-
GCC_optimization_-_Gentoo_Wiki.pdf ↩