Here’s two more C compiler options I looked at. Hold on to the edge of your seats folks.
-fpeel-loops
Definition from the GNU man page:
Peels the loops for that there is enough information that they do not roll much (from profile feedback). It also turns on complete loop peeling (i.e. complete removal of loops with small constant number of iterations).
Essentially, this option is one way to optimize for-loops by “peeling”. Peeling is the action of splitting the first or last few iterations from a loop and running them outside of the loop body. It is enabled with the -fprofile-use option, which allows the compiler to “profile” your code by reading it multiple times with feedback to learn how to optimize it optimally.
Here is an example of how peeling works.
BEFORE PEELING:
int p = 10;
for (int i=0; i<10; ++i) {
y[i] = x[i] + x[p];
p = i;
}
You can see p = 10 only for the first iteration. For the rest of the iterations, p = i – 1. Therefore, the first iteration can be “peeled”.
AFTER PEELING:
y[0] = x[0] + x[10];
for (int i=1; i<10; ++i){
y[i] = x[i] + x[i-1];
}
By peeling, we have eliminated the need for the variable p, which increased performance.
In summary, -fpeel-loops will optimize loops and increase your build time, but decrease run time. Therefore, it is useful in situations where run time is more important than the build time such as if the program will be distributed to users and consumers.
-funsafe-math-optimizations
Definition from the GNU man page:
Allow optimizations for floating-point arithmetic that (a) assume that arguments and results are valid and (b) may violate IEEE or ANSI standards. When used at link-time, it may include libraries or startup files that change the default FPU control word or other similar optimizations.
This option should never be turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications for math functions.
The default is -fno-unsafe-math-optimizations.
As the title states, this options makes some “unsafe math optimizations” – specifically with floating-point arithmetic. Seems unsafe right? It depends. Are you a mad rocket scientist trying to blow up an asteroid with laser beams where the tiniest of fractions and signed and unsigned zeroes could destroy the earth instead? Me neither. So you’re probably okay to use it. You may lose a few tiny decimal points, but if strictly adhering to IEEE/ANSI standards or being extremely precise isn’t important, then it won’t matter. So to hell with unsafe. Go ahead and try it. Maybe compare the output to make sure though…