Why don't modern C++ compilers optimize away simple loops like this? (Clang, MSVC)

前端 未结 4 1006
时光说笑
时光说笑 2021-01-07 17:31

When I compile and run this code with Clang (-O3) or MSVC (/O2)...

#include 
#include 

static int con         


        
4条回答
  •  一个人的身影
    2021-01-07 18:31

    The undefined behavior is irrelevant here. Replacing the inner loop with:

        for (int j = 1; j < N; ++j)
        {
            a[j-1] = a[j];
            a[j] = j;
        }
    

    ... has the same effect, at least with Clang.

    The issue is that the inner loop both loads from a[j] (for some j) and stores to a[j] (for some j). None of the stores can be removed, because the compiler believes they may be visible to later loads, and none of the loads can be removed, because their values are used (as input to the later stores). As a result, the loop still has side-effects on memory, so the compiler doesn't see that it can be deleted.

    Contrary to n.m.'s answer, replacing int with unsigned does not make the problem go away. The code generated by Clang 3.4.1 using int and using unsigned int is identical.

提交回复
热议问题