2017-07-30 9 views
0

C++インライン最適化

func(int): 
    imul edi, edi 
    mov eax, edi 
    ret 
main: 
    push rbx 
    mov rdi, QWORD PTR [rsi+8] 
    mov rbx, rsi 
    mov edx, 10 
    xor esi, esi 
    call strtol 
    imul eax, eax 
    mov edi, OFFSET FLAT:std::cout 
    mov esi, eax 
    call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) 
    mov rdi, QWORD PTR [rbx+16] 
    mov edx, 10 
    xor esi, esi 
    call strtol 
    imul eax, eax 
    mov edi, OFFSET FLAT:std::cout 
    mov esi, eax 
    call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) 
    mov eax, 1 
    pop rbx 
    ret 
_GLOBAL__sub_I__Z4funci: 
    sub rsp, 8 
    mov edi, OFFSET FLAT:std::__ioinit 
    call std::ios_base::Init::Init() 
    mov edx, OFFSET FLAT:__dso_handle 
    mov esi, OFFSET FLAT:std::__ioinit 
    mov edi, OFFSET FLAT:std::ios_base::Init::~Init() 
    add rsp, 8 
    jmp __cxa_atexit 

EXPLICIT INLINEに私の例here OR 次のC++ &マッチングアセンブリ

IMPLICIT INLINE

#include <iostream> 

int func(int i) 
{ 
    return i * i; 
} 

int main(int argc, char *argv[]) { 

    auto value = atoi(argv[1]); 
    std::cout << func(value); 


    value = atoi(argv[2]); 
    std::cout << func(value); 

    return 1; 
} 

結果を参照してくださいライン5がコメントアウトされている場合の例で

main: 
    push rbx 
    mov rdi, QWORD PTR [rsi+8] 
    mov rbx, rsi 
    mov edx, 10 
    xor esi, esi 
    call strtol 
    imul eax, eax 
    mov edi, OFFSET FLAT:std::cout 
    mov esi, eax 
    call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) 
    mov rdi, QWORD PTR [rbx+16] 
    mov edx, 10 
    xor esi, esi 
    call strtol 
    imul eax, eax 
    mov edi, OFFSET FLAT:std::cout 
    mov esi, eax 
    call std::basic_ostream<char, std::char_traits<char> >::operator<<(int) 
    mov eax, 1 
    pop rbx 
    ret 
_GLOBAL__sub_I_main: 
    sub rsp, 8 
    mov edi, OFFSET FLAT:std::__ioinit 
    call std::ios_base::Init::Init() 
    mov edx, OFFSET FLAT:__dso_handle 
    mov esi, OFFSET FLAT:std::__ioinit 
    mov edi, OFFSET FLAT:std::ios_base::Init::~Init() 
    add rsp, 8 
    jmp __cxa_atexit 
#include <iostream> 

inline int func(int i) 
{ 
    return i * i; 
} 

int main(int argc, char *argv[]) { 

    auto value = atoi(argv[1]); 
    std::cout << func(value); 


    value = atoi(argv[2]); 
    std::cout << func(value); 

    return 1; 
} 

結果、コードの最適化は、2つのコール部位で機能「FUNC」をインラインが、それは、アセンブリを残し生成されたバイナリでfuncを実行します。ただし、 'func'が明示的にインライン化されている場合、関数は出力アセンブリに存在しません。

GCCオプティマイザは、インライン関数のオペレーションが呼び出しコードで真にインライン化されているにもかかわらず、コンパイルされたアセンブリ内に暗黙的にインライン関数を残すのはなぜですか?

+0

ここにコードを記入してご質問ください。私たちの助けが必要な場合は、私たちにそれを簡単にするという礼儀をするべきです。 –

+0

アセンブリはどうですか?私がGodboltを使用した理由は何が起こっているかを実証することが非常に簡単であるからです。 – tuskcode

+0

郵便番号は直接質問にあります。 godboltのような外部リソースへのリンクは素敵なものです。コードとそれへのリンクの両方を投稿することには何も問題ありません。 –

答えて

0

staticとマークされていると、この機能は完全に終了します。現在のところstaticと表示されていないため、別のコンパイル単位で参照される可能性があるため、削除することはできません。