clang编译c语言开o优化,针对gcc或clang的LTO可以跨C和C方法进行优化
是!
鏈接時優(yōu)化通常適用于“胖”目標(biāo)文件中存在的中間表示(IR),其可以包含用于傳統(tǒng)鏈接的機(jī)器代碼和用于LTO鏈接的IR.
在這個階段,沒有更高級的語言結(jié)構(gòu),因此鏈接時優(yōu)化與語言無關(guān).
GCC
海灣合作委員會的link-time optimization(LTO)在GCC的中間代表之一GIMPLE上運作. IR始終與語言無關(guān),因此任何鏈接時優(yōu)化都可以在任何語言生成的代碼中使用.
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.
這是一個示例,向您展示這項技術(shù)的強(qiáng)大功能.我們將定義一個C函數(shù)并從C程序中調(diào)用它:
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()內(nèi)聯(lián)到我的C main()中,而且它將整個事物變成了一個常量表達(dá)式!
Clang / LLVM
使用相同的語法,Clang能夠使用LLVM IR發(fā)出“胖”對象文件,這可以在鏈接時進(jìn)行優(yōu)化.見LLVM Link Time Optimization.
使用與上面相同的測試代碼,clang產(chǎn)生完全相同的結(jié)果:
00000000004004b0 :
4004b0: b8 0a 00 00 00 mov eax,0xa
4004b5: c3 ret
總結(jié)
以上是生活随笔為你收集整理的clang编译c语言开o优化,针对gcc或clang的LTO可以跨C和C方法进行优化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存升级,双通道加速
- 下一篇: 软件开发中的内存读写性能提升秘籍