In the file file1.c
, there is a call to a function that is implemented in the file file2.c
.
When I link file1.o
and file2.o
into an executable, if the function in file2
is very small, will the linker automatically detect that the function is small and inline its call?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
In addition to the support for Link Time Code Generation (LTCG) that Jame McNellis mentioned, the GCC toolchain also supports link time optimization. Starting with version 4.5, GCC supports the
-flto
switch which enables Link Time Optimization (LTO), a form of whole program optimization that lets it inline functions from separate object files (and whatever other optimizations a compiler might be able to make if it were compiling all the object files as if they were from a single C source file).Here's a simple example:
test.c:
print_int.c:
First compile them using GCC4.5.x - examples from GCC docs use
-O2
, but to get visible results in my simple test, I had to use-O3
:To get the effect of LTO you're supposed to use the optimization options even at the link stage - the linker actually invokes the compiler to compile pieces of intermediate code that the compiler put into the object file in the first steps above. If you don't pass the optimization option at this stage as well, the compiler won't perform the inlining that you'd be looking for.
Disassembly of the version without link time optimization. Note that the calls are made to the
print_int()
function:Disassembly of the version with link time optimization. Note that the calls to
printf()
are made directly:And here's the same experiment with MSVC (first with LTCG):
Now without LTCG. Note that with MSVC you have to compile the .c file without the
/GL
to prevent the linker from performing LTCG - otherwise the linker detects that/GL
was specified, and it'll force the/LTCG
option (hey, that's what you said you wanted the first time around with/GL
):One thing that Microsoft's linker supports in LTCG
that is not supported by GCC (as far as I know)is Profile Guided Optimization (PGO). That technology allows Microsoft's linker to optimize based on a profiling data gathered from previous runs of the program. This allows the linker to do things such as gather 'hot' functions onto the same memory pages and seldom used code sequences onto other memory pages to reduce the working set of a program.Edit (28 Aug 2011): GCC support profile guided optimization using such options as
-fprofile-generate
and-fprofile-use
, but I'm completely uninformed about them.Thanks to Konrad Rudolph for pointing this out to me.