関数がインライン化されるか確かめる

http://d.hatena.ne.jp/shinichiro_h/20071216#1197793386
libcとかリンクすると探しにくいので、自分は「gcc -S」派です。

struct S
{
    explicit S(int x) : x_(x) {}
    int x() const { return x_; }
    int x_;
};

int hoge(const S& s)
{
    return s.x();
}

main()を使わないのは処理系によってはmain()の先頭と末尾に余分なコードを付けたりするからです。
あとピンポイントでインライン化を確認するため、オブジェクトの参照を引数にしています。


で、これを「g++ -S -o - inline.cpp | c++filt」とかします。

        .file   "inline.cpp"
        .text
        .align 2
.globl hoge(S const&)
        .def    hoge(S const&); .scl    2;      .type   32;     .endef
hoge(S const&):
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    8(%ebp), %eax
        movl    %eax, (%esp)
        call    S::x() const
        leave
        ret
        .section        .text$_ZNK1S1xEv,"x"
        .linkonce discard
        .align 2
.globl S::x() const
        .def    S::x() const;   .scl    2;      .type   32;     .endef
S::x() const:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        movl    (%eax), %eax
        popl    %ebp
        ret

「-o -」で標準出力に吐いて、c++filtでデマングルしてます。


「g++ -S -O2 -o - inline.cpp | c++filt」ならこうなります。

        .file   "inline.cpp"
        .text
        .align 2
        .p2align 4,,15
.globl hoge(S const&)
        .def    hoge(S const&); .scl    2;      .type   32;     .endef
hoge(S const&):
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        popl    %ebp
        movl    (%eax), %eax
        ret

まぁ、出力が読みやすいかどうかは好みの問題ですが、、、。
あと、リンク時最適化とかはobjdumpしないと分からないので、その辺は臨機応変に。