我想看看如何某些C/C++的功能被翻譯成彙編和我創建以下文件:爲什麼gcc創建冗餘彙編代碼?
struct foo {
int x;
char y[0];
};
char *bar(struct foo *f)
{
return f->y;
}
然後,我gcc -S
編譯這一點(也試圖與g++ -S
),但是當我看着彙編代碼,我很失望地發現,我認爲gcc
應該能夠優化掉酒吧功能的瑣碎冗餘:
_bar:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
movq %rdi, -8(%rbp)
movq -8(%rbp), %rax
movabsq $4, %rcx
addq %rcx, %rax
movq %rax, -24(%rbp)
movq -24(%rbp), %rax
movq %rax, -16(%rbp)
movq -16(%rbp), %rax
popq %rbp
ret
Leh_func_end1:
除其他事項外,該行
movq %rax, -24(%rbp)
movq -24(%rbp), %rax
movq %rax, -16(%rbp)
movq -16(%rbp), %rax
顯得毫無意義的多餘。 gcc(可能還有其他編譯器)是否有任何理由不能/不會優化它呢?
請使用-O開關運行gcc以啓用標準優化。 –
您使用的是哪個版本的gcc? –