Link-time optimization typically works on intermediate representation (IR) present in "fat" object files, which can contain both machine code for traditional linking, and IR for LTO linking.
At this stage, there are no more high-level language constructs, so link-time-optimization is language-agnostic.
GCC
GCC's link-time optimization (LTO) works on GIMPLE, one of GCC's intermediate representations. The IR is always language-agnostic, so any link-time optimizations will work across code generated from any language.
Notice that the final link is done with g++ to get the C++ runtime libraries and -lgfortran is added to get the Fortran runtime libraries. In general, when mixing languages in LTO mode, you should use the same link command options as when mixing languages in a regular (non-LTO) compilation.
Here's an example to show you just how powerful this technology is. We'll define a C function and call it from a C++ program:
func.h
#ifndef FUNC_DOT_H
#define FUNC_DOT_H
#ifdef __cplusplus
extern "C" {
#endif
int func(int a, int b, int c);
#ifdef __cplusplus
}
#endif
#endif /* FUNC_DOT_H */
func.c
#include "func.h"
int func(int a, int b, int c)
{
return 3*a + 2*b + c;
}
main.cpp
#include "func.h"
int main()
{
int a = 1;
int b = 2;
int c = 3;
return func(a, b, c);
}
You can see that it not only inlined my C func() into my C++ main(), but it turned the whole thing into a constant expression!
Clang / LLVM
Using the same syntax, Clang is capable of emitting "fat" object files with LLVM IR, which can be optimized during link time. See LLVM Link Time Optimization.
Using the same test code as above, clang produces the exact same result:
Yes!
Link-time optimization typically works on intermediate representation (IR) present in "fat" object files, which can contain both machine code for traditional linking, and IR for LTO linking.
At this stage, there are no more high-level language constructs, so link-time-optimization is language-agnostic.
GCC
GCC's link-time optimization (LTO) works on GIMPLE, one of GCC's intermediate representations. The IR is always language-agnostic, so any link-time optimizations will work across code generated from any language.
From the GCC Optimization Options documentation:
Here's an example to show you just how powerful this technology is. We'll define a C function and call it from a C++ program:
func.h
func.c
main.cpp
Compile
Disassemble (
objdump -Mintel -d -R -C testlto
)You can see that it not only inlined my C
func()
into my C++main()
, but it turned the whole thing into a constant expression!Clang / LLVM
Using the same syntax, Clang is capable of emitting "fat" object files with LLVM IR, which can be optimized during link time. See LLVM Link Time Optimization.
Using the same test code as above, clang produces the exact same result: