Revisiting the C compiler optimizations
Following up on my previous article on the C compilation pipeline, we will now focus on the effects of compiler optimizations, how they affect the generated executable as well as its consequences on the ability to debug a program.
The compiler, depending on the desired optimization level, will apply a set of increasingly agressive techniques to transform a program's source code to increasingly more efficient machine instructions, speeding up execution and reducing resource consumption. There are no free lunches however, each transformation for the sake of performance slows the compilation process and risks of making the generated machine code less transparent and more challenging to debug.
Modern compilers are sophisticated tools that do far more than simply translate human-readable code into machine instructions. They analyze, transform, and optimize code in ways that can dramatically differ from the original source. For developers, this means the code running on a machine might look quite different from the original source code. We'll use a simple example program to demonstrate how compiler optimizations can fundamentally affect the generated machine code, explore the impact on the ability to debug a running program.