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

前端 未结 4 963
时光说笑
时光说笑 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

    It's an interesting issue with regards to optimizing. I would expect that in most cases, the compiler would treat each element of the array as an individual variable when doing dead code analysis. Ans 0x8000 make too many individual variables to track, so the compiler doesn't try. The fact that a[j] doesn't always access the the same object could cause problems as well for the optimizer.

    Obviously, different compilers use different heuristics; a compiler could treat the array as a single object, and detect that it never affected output (observable behavior). Some compilers may choose not to, however, on the grounds that typically, it's a lot of work for very little gain: how often would such optimizations be applicable in real code?

提交回复
热议问题