C 链接器是否可以内联函数?
在文件C 链接器是否可以内联函数?,c,optimization,linker,inline,C,Optimization,Linker,Inline,在文件file1.c中,调用了在文件file2.c中实现的函数。 当我将file1.o和file2.o链接到一个可执行文件中时,如果file2中的函数非常小,链接器是否会自动检测到该函数很小并内联其调用?除了Jame McNell提到的对链接时间代码生成(LTCG)的支持之外,GCC工具链还支持链接时间优化。从4.5版开始,GCC支持-flto开关,该开关启用链接时间优化(LTO),这是一种全程序优化形式,允许它从单独的目标文件内联函数(如果编译器编译所有的对象文件,就好像它们来自一个C源文件一
file1.c
中,调用了在文件file2.c
中实现的函数。
当我将
file1.o
和file2.o
链接到一个可执行文件中时,如果file2
中的函数非常小,链接器是否会自动检测到该函数很小并内联其调用?除了Jame McNell提到的对链接时间代码生成(LTCG)的支持之外,GCC工具链还支持链接时间优化。从4.5版开始,GCC支持-flto
开关,该开关启用链接时间优化(LTO),这是一种全程序优化形式,允许它从单独的目标文件内联函数(如果编译器编译所有的对象文件,就好像它们来自一个C源文件一样,那么它可以进行任何其他优化)
下面是一个简单的例子:
测试.c:
void print_int(int x);
int main(){
print_int(1);
print_int(42);
print_int(-1);
return 0;
}
#include <stdio.h>
void print_int( int x)
{
printf( "the int is %d\n", x);
}
打印int.c:
void print_int(int x);
int main(){
print_int(1);
print_int(42);
print_int(-1);
return 0;
}
#include <stdio.h>
void print_int( int x)
{
printf( "the int is %d\n", x);
}
为了获得LTO的效果,即使在链接阶段,你也应该使用优化选项——链接器实际上调用编译器来编译编译器在上面的第一步中放入目标文件的中间代码。如果你在这个阶段没有通过优化选项,编译器将不会执行in你要找的衬里
# link using LTO
C:\temp>gcc -o test-lto.exe -flto -O3 print_int.o test.o
在没有链接时间优化的情况下反汇编版本。请注意,调用的是print\u int()
函数:
C:\temp>gdb test-nolto.exe
GNU gdb (GDB) 7.2
(gdb) start
Temporary breakpoint 1 at 0x401373
Starting program: C:\temp/test-nolto.exe
[New Thread 3324.0xdc0]
Temporary breakpoint 1, 0x00401373 in main ()
(gdb) disassem
Dump of assembler code for function main:
0x00401370 <+0>: push %ebp
0x00401371 <+1>: mov %esp,%ebp
=> 0x00401373 <+3>: and $0xfffffff0,%esp
0x00401376 <+6>: sub $0x10,%esp
0x00401379 <+9>: call 0x4018ca <__main>
0x0040137e <+14>: movl $0x1,(%esp)
0x00401385 <+21>: call 0x401350 <print_int>
0x0040138a <+26>: movl $0x2a,(%esp)
0x00401391 <+33>: call 0x401350 <print_int>
0x00401396 <+38>: movl $0xffffffff,(%esp)
0x0040139d <+45>: call 0x401350 <print_int>
0x004013a2 <+50>: xor %eax,%eax
0x004013a4 <+52>: leave
0x004013a5 <+53>: ret
下面是MSVC的相同实验(首先是LTCG):
现在没有LTCG。请注意,使用MSVC,您必须在不使用/GL
的情况下编译.c文件,以防止链接器执行LTCG-否则链接器会检测到指定了/GL
,它将强制执行/LTCG
选项(嘿,这就是您第一次使用/GL
时想要的):
微软的链接器在LTCG中支持的一件事是GCC不支持的(据我所知),那就是配置文件引导优化(PGO)。该技术允许Microsoft的链接器根据从以前运行的程序中收集的分析数据进行优化。这允许链接器在同一内存页上收集“热”函数,并在其他内存页上收集很少使用的代码序列,以减少程序的工作集
编辑(2011年8月28日):GCC支持使用
-fprofile-generate
和-fprofile-use
等选项进行概要文件引导优化,但我对此一无所知
感谢Konrad Rudolph指出这一点。
一些链接器可以,是的(VisualC++链接器有一个叫做链接时间代码生成的特性”,它执行交叉模块内联和优化。不管你使用的链接器是否可以,都不可能说,因为你还没有告诉我们你使用的链接器。(即使这样,唯一能确定的方法是找出链接器生成的代码…)。GCC也支持PGO,通过-fprofile generate
和-fprofile use
@Konrad:wow-我完全不知道。我将不得不研究它。谢谢!但LTO不是由链接器(binutils/ld)处理的;它是一个编译器(GCC/GCC)@osgx:没错,但它实际上是由collect2
实现的,这是一种预链接器或链接器包装器()。另请参见
C:\temp>cl -c /GL /Zi /Ox test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.c
C:\temp>cl -c /GL /Zi /Ox print_int.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
print_int.c
C:\temp>link /LTCG test.obj print_int.obj /out:test-ltcg.exe /debug
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Generating code
Finished generating code
C:\temp>"\Program Files (x86)\Debugging Tools for Windows (x86)"\cdb test-ltcg.exe
Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
Copyright (c) Microsoft Corporation. All rights reserved.
CommandLine: test-ltcg.exe
// ...
0:000> u main
*** WARNING: Unable to verify checksum for test-ltcg.exe
test_ltcg!main:
00cd1c20 6a01 push 1
00cd1c22 68d05dcd00 push offset test_ltcg!__decimal_point_length+0x10 (00cd5dd0)
00cd1c27 e8e3f3feff call test_ltcg!printf (00cc100f)
00cd1c2c 6a2a push 2Ah
00cd1c2e 68d05dcd00 push offset test_ltcg!__decimal_point_length+0x10 (00cd5dd0)
00cd1c33 e8d7f3feff call test_ltcg!printf (00cc100f)
00cd1c38 6aff push 0FFFFFFFFh
00cd1c3a 68d05dcd00 push offset test_ltcg!__decimal_point_length+0x10 (00cd5dd0)
00cd1c3f e8cbf3feff call test_ltcg!printf (00cc100f)
00cd1c44 83c418 add esp,18h
00cd1c47 33c0 xor eax,eax
00cd1c49 c3 ret
0:000>
C:\temp>cl -c /Zi /Ox test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.c
C:\temp>cl -c /Zi /Ox print_int.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
print_int.c
C:\temp>link test.obj print_int.obj /out:test-noltcg.exe /debug
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
C:\temp>"\Program Files (x86)\Debugging Tools for Windows (x86)"\cdb test-noltcg.exe
Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
Copyright (c) Microsoft Corporation. All rights reserved.
CommandLine: test-noltcg.exe
// ...
0:000> u main
test_noltcg!main:
00c41020 6a01 push 1
00c41022 e8e3ffffff call test_noltcg!ILT+5(_print_int) (00c4100a)
00c41027 6a2a push 2Ah
00c41029 e8dcffffff call test_noltcg!ILT+5(_print_int) (00c4100a)
00c4102e 6aff push 0FFFFFFFFh
00c41030 e8d5ffffff call test_noltcg!ILT+5(_print_int) (00c4100a)
00c41035 83c40c add esp,0Ch
00c41038 33c0 xor eax,eax
00c4103a c3 ret
0:000>