6

我在 gcc 中遇到了一个非常奇怪的行为,涉及标有__attribute((const)). 逻辑和算术运算符导致不同的优化,我不明白为什么。

这并不是一个真正的错误,因为__attribute((const))它只是一个提示并且不能保证它的效果,但这仍然是非常令人惊讶的。有人有什么解释吗?

这是代码。所以我定义了一个__attribute((const))函数:

int f(int & counter) __attribute((const));
int f(int & counter) {
    ++counter;
    return 0;
}

然后我定义了一个算子测试宏。这是通过宏而不是模板/仿函数来完成的,以向编译器提供简单的代码并简化优化:

int global = 0; // forces results to be computed

#define TestOp(OP) \
    { \
        int n = 0; \
        global += (f(n) OP f(n)); \
        std::cout << "op" #OP " calls f " << n << " times" << std::endl; \
    }

最后,我测试了不同的运算符,如下所示。注释将输出与在和的g++-4.8 -std=c++11 -O2 -Wall -pedantic相同输出相匹配-O3-Ofast

int main() {
    // all calls optimized away
    TestOp(^)   // 0
    TestOp(-)   // 0
    // one call is optimized away
    TestOp(|)   // 1
    TestOp(&)   // 1
    TestOp(||)  // 1
    TestOp(&&)  // 1
    // no optimization
    TestOp(+)   // 2
    TestOp(*)   // 2

    return global;
}

我的问题是:为什么算术运算符会产生两个调用?为什么不能f()+f()优化为2*f()?有没有办法帮助/强制这种优化?起初我认为乘法可能更昂贵,但我尝试了f()+....+f()10 次加法仍然没有减少到10*f(). 此外,由于它是int算术运算,因此操作顺序无关紧要(与floats 相反)。

我还检查了 asm,但它没有帮助:所有整数似乎都是在编译时预先计算的。

4

1 回答 1

5

编译器不信任你。由于您有一个引用参数,编译器似乎不信任您的const属性 - 一个const函数应该只查看通过参数传递的值(而不是引用或取消引用指针)。

另一种测试方法是const在单独的编译单元中分解函数:

测试1.cpp:

#include <stdio.h>
int global = 0; // forces results to be computed

int f(int i) __attribute((const));
void print_count(void);

#define TestOp(OP) \
    { \
        int n = 0; \
        global += (f(n) OP f(n)); \
        printf("op %s ", #OP);\
        print_count();\
    }

int main() {
    // all calls optimized away
    TestOp(^)   // 0
    TestOp(-)   // 0
    // one call is optimized away
    TestOp(|)   // 1
    TestOp(&)   // 1
    TestOp(||)  // 1
    TestOp(&&)  // 1
    // no optimization
    TestOp(+)   // 2
    TestOp(*)   // 2

    return global;
}

计数器.cpp:

#include <stdio.h>
static int counter = 0;

int f(int i) {
    ++counter;
    return 0;
}

void print_count(void)
{
   printf("counter %d\n", counter);
    counter = 0;
}

现在编译器发现不需要调用f(0)until f(0) | f(0),并且该调用的结果将f(0)重新用于其他情况。

$ g++ -O2 -c counter.cpp && g++ -O2 -c test.cpp && g++ counter.o test.o && ./a.out
op ^ counter 0
op - counter 0
op | counter 1
op & counter 0
op || counter 0
op && counter 0
op + counter 0
op * counter 0
于 2013-12-02T14:15:40.280 回答