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
總結
以上是生活随笔為你收集整理的clang编译c语言开o优化,针对gcc或clang的LTO可以跨C和C方法进行优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存升级,双通道加速
- 下一篇: c语言两个变量相乘出现乱码,C语言,矩阵