29/07/2010
Whole Program Optimization
Not long ago, I was compiling some library with Visual C++ 2008 and I noticed that the release version was noticeably larger than the debug version. The culprit was “link time code generation” (LCTG), the mechanism behind the “whole program optimization” option of Visual C++. But what is it doing exactly?
When you build a project, Visual C++ usually goes through three stages:
- The frontend, which parses the source code and builds a graph-like structure called the “intermediate representation” (IR).
- The backend, which compiles the IR into assembly code.
- The linker, which glues together all the compiled parts of the program.
Usually, a library is nothing more than a collection of compiled chunks, somewhere between steps 2 and 3. But LCTG allows libraries to store IR instead of compiled code. The advantage in keeping the structure is that cross-unit optimizations are still possible at link time (like function inlining).
This page explains the mechanism inside-out: Under The Hood - Link-time Code Generation (link)