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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值