Why does adding inline assembly comments cause such radical change in GCC's generated code?

前端 未结 4 2017
无人共我
无人共我 2020-12-12 20:08

So, I had this code:

constexpr unsigned N = 1000;
void f1(char* sum, char* a, char* b) {
    for(int i = 0; i < N; ++i) {
        sum[i] = a[i] + b[i];
           


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 20:48

    Note that gcc vectorized the code, splitting the loop body into two parts, the first processing 16 items at a time, and the second doing the remainder later.

    As Ira commented, the compiler doesn't parse the asm block, so it does not know that it's just a comment. Even if it did, it has no way of knowing what you intended. The optmized loops have the body doubled, should it put your asm in each? Would you like it that it isn't executed 1000 times? It doesn't know, so it goes the safe route and falls back to the simple single loop.

提交回复
热议问题