日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/2/28 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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方法进行优化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。