Visual C++〜不内联简单的const函数指针调用

9 c++ function-pointers

亲爱的StackOverflowers,

我在Microsoft Visual Studio C++ 2012上编写了一段简单的代码:

int add(int x, int y)
{
    return x + y;
}

typedef int (*func_t)(int, int);

class A
{
public:
    const static func_t FP;
};

const func_t A::FP = &add;

int main()
{
 int x = 3;
 int y = 2;
 int z = A::FP(x, y);
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器生成以下代码:

int main()
{
000000013FBA2430  sub         rsp,28h  
int x = 3;
int y = 2;
int z = A::FP(x, y);
000000013FBA2434  mov         edx,2  
000000013FBA2439  lea         ecx,[rdx+1]  
000000013FBA243C  call        qword ptr [A::FP (013FBA45C0h)]  
return 0;
000000013FBA2442  xor         eax,eax
}
Run Code Online (Sandbox Code Playgroud)

我在内联函数扩展的'完全优化'(/ Obx标志)和'任何适合'上编译了这个.(/ Ob2标志)

我想知道为什么编译器不会内联这个调用,特别是因为它是const.你们中有没有人知道它为什么没有内联,是否有可能使编译器内联呢?

基督教

编辑:我现在正在运行一些测试,MSVC在以下情况下也无法内联函数指针:

- 我将const指针移出类并使其成为全局.

- 我将const指针移出类,并使其在main中本地化.

- 我使指针非常量并在本地移动它.

- 当我使返回类型为void并且没有给出参数时

我开始认为Microsoft Visual Studio根本无法内联函数指针...

Ben*_*igt 2

问题不在于内联,编译器一有机会就会内联。问题是 Visual C++ 似乎没有意识到指针变量实际上是一个编译时常量。

测试用例:

// function_pointer_resolution.cpp : Defines the entry point for the console application.
//

extern void show_int( int );

extern "C" typedef int binary_int_func( int, int );

extern "C" binary_int_func sum;
extern "C" binary_int_func* const sum_ptr = sum;

inline int call( binary_int_func* binary, int a, int b ) { return (*binary)(a, b); }

template< binary_int_func* binary >
inline int callt( int a, int b ) { return (*binary)(a, b); }

int main( void )
{
    show_int( sum(1, 2) );
    show_int( call(&sum, 3, 4) );
    show_int( callt<&sum>(5, 6) );
    show_int( (*sum_ptr)(1, 7) );
    show_int( call(sum_ptr, 3, 8) );
//  show_int( callt<sum_ptr>(5, 9) );
    return 0;
}

// sum.cpp
extern "C" int sum( int x, int y )
{
    return x + y;
}

// show_int.cpp
#include <iostream>

void show_int( int n )
{
    std::cout << n << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这些函数被分成多个编译单元,以便更好地控制内联。具体来说,我不想show_int内联,因为它会使汇编代码变得混乱。

第一个麻烦是有效代码(注释行)被 Visual C++ 拒绝。 G++ 对此没有问题,但 Visual C++ 抱怨“预期的编译时常量表达式”。这实际上是所有未来行为的良好预测。

在启用优化和正常编译语义(无跨模块内联)的情况下,编译器生成:

_main   PROC                        ; COMDAT

; 18   :    show_int( sum(1, 2) );

    push    2
    push    1
    call    _sum
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int

; 19   :    show_int( call(&sum, 3, 4) );

    push    4
    push    3
    call    _sum
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int

; 20   :    show_int( callt<&sum>(5, 6) );

    push    6
    push    5
    call    _sum
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int

; 21   :    show_int( (*sum_ptr)(1, 7) );

    push    7
    push    1
    call    DWORD PTR _sum_ptr
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int

; 22   :    show_int( call(sum_ptr, 3, 8) );

    push    8
    push    3
    call    DWORD PTR _sum_ptr
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int
    add esp, 60                 ; 0000003cH

; 23   :    //show_int( callt<sum_ptr>(5, 9) );
; 24   :    return 0;

    xor eax, eax

; 25   : }

    ret 0
_main   ENDP
Run Code Online (Sandbox Code Playgroud)

sum_ptr使用和不使用之间已经存在巨大差异sum_ptr。使用语句sum_ptr生成间接函数调用call DWORD PTR _sum_ptr,而所有其他语句生成直接函数调用call _sum,即使源代码使用函数指针也是如此。

如果我们现在通过用 编译 function_pointer_resolution.cpp 和 sum.cpp 并用/GL链接来启用内联/LTCG,我们会发现编译器内联了所有直接调用。间接调用保持原样。

_main   PROC                        ; COMDAT

; 18   :    show_int( sum(1, 2) );

    push    3
    call    ?show_int@@YAXH@Z           ; show_int

; 19   :    show_int( call(&sum, 3, 4) );

    push    7
    call    ?show_int@@YAXH@Z           ; show_int

; 20   :    show_int( callt<&sum>(5, 6) );

    push    11                  ; 0000000bH
    call    ?show_int@@YAXH@Z           ; show_int

; 21   :    show_int( (*sum_ptr)(1, 7) );

    push    7
    push    1
    call    DWORD PTR _sum_ptr
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int

; 22   :    show_int( call(sum_ptr, 3, 8) );

    push    8
    push    3
    call    DWORD PTR _sum_ptr
    push    eax
    call    ?show_int@@YAXH@Z           ; show_int
    add esp, 36                 ; 00000024H

; 23   :    //show_int( callt<sum_ptr>(5, 9) );
; 24   :    return 0;

    xor eax, eax

; 25   : }

    ret 0
_main   ENDP
Run Code Online (Sandbox Code Playgroud)

底线:是的,编译器会通过编译时常量函数指针进行内联调用,只要该函数指针不是从变量中读取的。 函数指针的使用得到了优化:

call(&sum, 3, 4);
Run Code Online (Sandbox Code Playgroud)

但这并没有:

(*sum_ptr)(1, 7);
Run Code Online (Sandbox Code Playgroud)

所有测试均使用 Visual C++ 2010 Service Pack 1 运行,针对 x86 进行编译,托管在 x64 上。

适用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器版本 16.00.40219.01