Elimination of unused functions

As you know, Link-time optimization can make decisions based on knowledge of all translation units. Let's assume the following example taken from Ruby configuration script:

The program tests if one can call __builtin_longjmp with a variable given as argument. With non-LTO mode a compilation error occurs:

If you build the same source file with -flto, there are no errors:

In LTO mode, the compiler proves that there is no usage of the symbol and the function is removed. Behavior is similar to keyword static that allows a compiler to do the same job even in non-LTO mode. To be sure a function symbol is not optimized out, add __attribute__((used)):

Symbol usage from assembly language

Even though usage of assembly language is quite rare, there are still many libraries/programs that contain asm statements. Performance critical parts written in assembler can be widely seen in audio/video codecs. Let's take following http://gcc.gnu.org/PR60690 (ffmpeg bug) as our example:

If we compile and link the translation unit with following options:

objdump presents:

On the other hand, if you add -flto option, a linker cannot perform position independent code:

Explanation is quite simple: even though the used attribute is added to the static variable, the compiler does not parse asm statements for a translation unit. As a result, LTO partitioning does not tie wm1010 and fn1 and the symbols go to different partitions.

Objdump of partitions follows:

ltrans0.s:

ltrans1.s:

As you can see, the generated objects cannot really be linked to fulfill -fPIC flag.

As a workaround you may use -fno-lto when compiling the files that contain the asm statements.

ar, nm and ranlib

If you try to build bigger projects with -flto you have to make sure that you use a version of binutils that supports gcc's liblto_plugin. Since version 4.9 gcc produces slim object files that only contain the intermediate representation. In order to handle archives of these objects you have to use the gcc wrappers: gcc-ar, gcc-nm and gcc-ranlib. (The next version of binutils will support automatic loading of the liblto_plugin.)

Let's take a simple example:

Creating an archive with gcc-ar works as expected:

If you use a version of ar that doesn't load the liblto_plugin, you'll get linker errors:

gcc uses standard ELF files with special LTO sections for storing its IR.

For example:

None: LinkTimeOptimizationFAQ (last edited 2014-09-06 10:56:28 by MarkusTrippelsdorf)