Whole program optimization (
WPO) is the compiler optimization of a program using information about all the
modules in the program. Normally, optimizations are performed on a
per module, "compiland", basis; but this approach, while easier to write and test and less demanding of resources during the compilation itself, does not allow certainty about the safety of a number of optimizations such as aggressive
inlining and thus cannot perform them even if they would actually turn out to be efficiency gains that do not change the
semantics of the emitted object code.
Link-time optimization (
LTO) is a type of program optimization performed by a compiler to a program at
link time. Link time optimization is relevant in programming languages that compile programs on a file-by-file basis, and then link those files together (such as
C and
Fortran), rather than all at once (such as
Java's
just-in-time compilation (JIT)). Once all files have been compiled separately into
object files, traditionally, a compiler links (merges) the object files into a single file, the
executable. However, in LTO as implemented by the
GNU Compiler Collection (GCC) and
LLVM, the compiler is able to dump its
intermediate representation (IR), i.e.
GIMPLE bytecode or LLVM bitcode, respectively, so that all the different compilation units that will go to make up a single executable can be optimized as a single module when the link finally happens. This expands the scope of interprocedural optimizations to encompass the whole program (or, rather, everything that is visible at link time). With link-time optimization, the compiler can apply various forms of interprocedural optimization to the whole program, allowing for deeper analysis, more optimization, and ultimately better program performance. In practice, LTO does not always optimize the entire program—
library functions, especially
dynamically linked shared objects, are intentionally kept out to avoid excessive duplication and to allow for updating.
Static linking does naturally lend to the concept of LTO, but it only works with library archives that contain IR objects as opposed to machine-code only object files. And of course, when the program being built is itself a library, the optimization would keep every externally-available (exported) symbol, without trying too hard at removing them as a part of DCE. ==History==