Usage
Option -flto=
You can enable LTO by adding this option to the compile command. You can use -flto=n to specify the number of concurrent LTOs. For example, you can set it to auto to specify the same number of concurrent LTOs as GNU make (if available) or the same as the number of threads on the current machine.
Option -ffat-lto-objects
This option generates object files containing both assembly code and LTO information. When -ffat-lto-objects is enabled, -fno-lto can be used to perform regular linking using the assembly code of the object file, bypassing LTO.
Option -flto-partition=1to1|balanced|max|one|none
This option controls the partitioning policies used to speed up compilation.
- 1to1: corresponds each source file to a partition.
- balanced: makes partitions roughly equal in size.
- max: creates a partition for each symbol.
- one: attempts to create a single partition.
- none: skips the entire program analysis and partitioning.
Option -flto-compression-level=n
This option controls the compression level of the IR information stored in the LTO object files.
Demo
1 2 3 4 5 | // four.c int four() { return 4; } |
1 2 3 4 5 | // five.c int five() { return 5; } |
1 2 3 4 5 6 7 | // test.c extern int four(); extern int five(); int main() { return four() + five(); } |
1 2 3 4 5 6 | gcc -O2 -flto test.c four.c five.c # or gcc -O2 -flto -c test.c gcc -O2 -flto -c four.c gcc -O2 -flto -c five.c gcc -flto test.o four.o five.o |
In the preceding case, when the executable file is generated using LTO, four() + five() is optimized to the immediate value 9 by inlining and constant folding, eliminating the overhead of function calls and runtime calculation. This optimization would not be possible without LTO. If LTO object files are generated only for four.c and test.c, the final result is 4 + five(), with only some LTO object files participating in local LTO.