![](/img/trans.png)
[英]Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?
[英]Can LLC -O=3 not omit frame pointers from unoptimized llvm bytecode produced by clang -O0?
我有一個簡單的 c 代碼:
// main.c
#include <stdio.h>
void foo()
{
}
int main()
{
return 0;
}
以下命令的 Output
clang -O3 -emit-llvm -c main.c -o main.bc; llc main.bc -o main.S; cat main.S;
我得到:
...
foo: # @foo
.cfi_startproc
# %bb.0:
retq
...
這是預期的。 foo()
function 已轉換為retq
指令。
但是如果我運行以下命令:
clang -emit-llvm -c main.c -o main.bc; llc -O=3 main.bc -o main.S; cat main.S;
我得到:
...
foo: # @foo
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
popq %rbp
.cfi_def_cfa %rsp, 8
retq
...
功能上這沒問題,但在這種情況下foo()
沒有必要處理空 function 的幀指針。 第一種情況和第二種情況的區別在於,在第一種情況下,我們使用 -O3 表示 clang,而在第二種情況下,我們使用 -O3 表示 llc。 我認為這兩個是等價的。
我還嘗試了以下方法:
clang -emit-llvm -fomit-frame-pointer -c main.c -o main.bc ; llc -O=3 main.bc -o main.S; cat main.S;
我得到:
...
foo: # @foo
.cfi_startproc
# %bb.0:
retq
...
那么,這是否意味着如果clang
決定將幀指針發送到 LLVM 字節碼中,那么llc
不夠聰明,無法刪除它們? ( llc
似乎沒有-fomit-frame-pointer
選項,盡管它確實有-disable-fp-elim
選項,它Disable frame pointer elimination optimization
,所以我認為 llc 能夠消除幀指針)
感謝@fuz
似乎 clang 在位碼中添加了禁用/啟用幀指針消除的屬性
當使用 -O0 編譯時,這些是位碼中的屬性:
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
使用 -O3 編譯時,這些是屬性:
attributes #0 = { norecurse nounwind readnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
看到no-frame-pointer-elim
的值在兩者中都不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.