Compiler Optimizations. Improved Performance.
Published: 2022-02-08 17:20:44

---



# Introduction

> Performance ≠ time. 

Compiler optimizations are alterations made to code by a compiler to achieve the same result as the original input code but with improved performance. This can mean reduced code size, reduced execution size, or improved execution speed; often, these goals are in conflict and optimizing for one (such as speed) will come at the cost of another (such as code size).

Most advanced compilers perform some level of optimization, and the options selected for compilation can have a significant effect on the trade-offs made by the compiler, **affecting memory usage, execution speed, executable size, power consumption, and debuggability.**

It is often possible to trade off performance in one area for another; using a lookup table, for example, can reduce CPU utilization and improve battery life in some algorithms, in return for increased memory consumption.

# GCC Optimization Options

GCC offers a large number of optimization options, most of which can be controlled in combination using the -O flag. This option accepts a value from 0 (no optimization) through 3 (highest optimization), s (optimize for size), fast (optimize for speed only), or g (optimize for debugging experience - avoid optimizations which convolute debugging).

The -O actually selects a set of features. These features may also be individually selected using the -f flag, followed by the feature name; for example, -funroll_loops enables the unrolling of loops (note: other features also affect loop unrolling). To disable a feature, use the -f flag followed by no- and the feature name; for example, -fno-unroll-loops turns off loop unrolling.

To see the individual features enabled by a particular -O flag, or combination of -O and feature flags, use the -Q --help=optimizers flags, which will query the optimization feature list. For example, to see all of the optimizations enabled/disabled at -O2, use this command:

```
gcc -O2 -Q --help=optimizers
```

The GCC documentation -- both the man page and even more so the online manuals -- have good information about the 200+ optimization features of the GCC compilers.

# Examples of Common Optimizations

Here are some common C optimizations. For each optimization, an input code snippet is shown, along with the code showing the equivalent of the optimization.

Note:

- There are many other kinds of optimizations, some of which operate at the object code level.
- Many of these optimizations cannot be performed when code has a side effect -- for example, the direction of a loop can sometimes be reversed without impact if the loop contains only calculations, but cannot be reversed if the loop contains I/O operations (such as printf()). Tracking these conditions can become complex!

# Code Rewriting Optimizations

These optimizations involve rewriting code for performance or space. Although these examples are shown using rewritten source code, in most cases the optimizations are applied to an intermediate representation of the program which does not closely resemble the original source.

```
- Strength Reduction
- Hoisting
- Hoisting I - Loop-Invariant Variable
- Hoisting II - Loop-Invariant Expression
- Hoisting III - Loop-Invariant Expression in Loop Condition
- Pre-calculation of Constants
- Loop Unswitching
- Loop Unswitching I - Inner/Outer Swap
- Loop Unswitching II - Inner/Outer Swap with Code Repetition
- Loop Splitting
- Loop Interchange
- Loop Unrolling
- Loop Unrolling I - Guaranteed-Multiple Iterations
- Loop Unrolling II - Pairs-of-Iterations plus a Conditional Extra Iteration
- Loop Unrolling III - Large Number of Iterations
- Inlining
- Common Subexpression Elimination
- Jump Threading 
```

This list can continue much longer and I don't want to make this a crazy long read blog post. If you would like to learn more regarding this topic. Please visit our website, for open source learning @[Seneca](https://wiki.cdot.senecacollege.ca/wiki/Compiler_Optimizations).

#Conclusion

⚠️ Repo for future first work: [here](https://twitter.com/aserputov/status/1490435738911088641)

⚠️ Ref for this blog: [here](https://wiki.cdot.senecacollege.ca/wiki/Compiler_Optimizations)

#Links

🖇 Follow me on [GitHub](https://github.com/aserputov) 

🖇 Follow me on [Twitter](https://twitter.com/aserputov) 

p.s This post was made for my SPO class, February Blog Posts /lab4