Optimizing across modules with link time optimization
At link time, more optimization opportunities are available because source code from different modules can be optimized together.
By default, the compiler optimizes each source module independently, translating C or C++ source code into an ELF file containing object code. At link time, the linker combines all the ELF object files into an executable by resolving symbol references and relocations. Compiling each source file separately means that the compiler might miss some optimization opportunities, such as cross-module inlining.
When link time optimization is enabled, the compiler translates source code into an intermediate form called LLVM bitcode. At link time, the linker collects all files containing bitcode together and sends them to the link time optimizer (
libLTO). Collecting modules together means that the link time optimizer can perform more optimizations because it has more information about the dependencies between modules. The link time optimizer then sends a single ELF object file back to the linker. Finally, the linker combines all object and library code to create an executable.
Figure 3-7 Link time optimization
In this figure, ELF Object containing Bitcode is an ELF file that does not contain normal code and data. Instead, it contains a section that is called
.llvmbc that holds LLVM bitcode.
.llvmbc is reserved. You must not create
.llvmbc section with, for example
CautionLink Time Optimization performs aggressive optimizations by analyzing the dependencies between bitcode format objects. This can result in the removal of unused variables and functions in the source code.
This section contains the following subsections: