3

我在这篇文章中使用的源代码也可以在这里找到:https ://gcc.godbolt.org/z/dGvxnv

鉴于此 C 源代码:

int pure_f(int a, int b) __attribute__((pure));

int const_f(int a, int b) __attribute__((const));

int my_f(int a, int b) {
    int x = pure_f(a, b);
    if (a > 0) {
        return x;
    }
    return a;
}

如果这是用 gcc with 编译的-O3,我希望 的评估pure_f(a, b)被移到if. 但它没有完成:

my_f(int, int):
        push    r12
        mov     r12d, edi
        call    pure_f(int, int)
        test    r12d, r12d
        cmovg   r12d, eax
        mov     eax, r12d
        pop     r12
        ret

另一方面,如果const_f被调用而不是pure_f,它被移动到if

my_f(int, int):
        test    edi, edi
        jg      .L4
        mov     eax, edi
        ret
.L4:
        jmp     const_f(int, int)

为什么不将此优化应用于纯函数?据我了解,这也应该是可能的,而且似乎是有益的。

- 编辑 -

GCC 错误报告(见评论):https ://gcc.gnu.org/bugzilla/show_bug.cgi?id=97307

4

0 回答 0