200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > clang编译c语言开o优化 针对gcc或clang的LTO可以跨C和C方法进行优化

clang编译c语言开o优化 针对gcc或clang的LTO可以跨C和C方法进行优化

时间:2021-03-05 19:23:28

相关推荐

clang编译c语言开o优化 针对gcc或clang的LTO可以跨C和C方法进行优化

是!

链接时优化通常适用于“胖”目标文件中存在的中间表示(IR),其可以包含用于传统链接的机器代码和用于LTO链接的IR.

在这个阶段,没有更高级的语言结构,因此链接时优化与语言无关.

GCC

海湾合作委员会的link-time optimization(LTO)在GCC的中间代表之一GIMPLE上运作. IR始终与语言无关,因此任何链接时优化都可以在任何语言生成的代码中使用.

Another feature of LTO is that it is possible to apply interprocedural optimizations on files written in different languages:

06000

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.

这是一个示例,向您展示这项技术的强大功能.我们将定义一个C函数并从C程序中调用它:

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);

}

gcc -o func.o -c -Wall -Werror -flto -O2 func.c

g++ -o main.o -c -Wall -Werror -flto -O2 main.cpp

g++ -o testlto -flto -O2 main.o func.o

拆解(objdump -Mintel -d -R -C testlto)

Disassembly of section .text:

00000000004003d0 :

4003d0: b8 0a 00 00 00 mov eax,0xa ; 1*3 + 2*2 + 3 = 10

4003d5: c3 ret

你可以看到它不仅将我的C func()内联到我的C main()中,而且它将整个事物变成了一个常量表达式!

Clang / LLVM

使用相同的语法,Clang能够使用LLVM IR发出“胖”对象文件,这可以在链接时进行优化.见LLVM Link Time Optimization.

使用与上面相同的测试代码,clang产生完全相同的结果:

00000000004004b0 :

4004b0: b8 0a 00 00 00 mov eax,0xa

4004b5: c3 ret

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。